Java 8 Int Stream使用StringBuilder进行收集 [英] Java 8 Int Stream collect with StringBuilder

查看:879
本文介绍了Java 8 Int Stream使用StringBuilder进行收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以创建一个int流,查找不同的字符,对它们进行排序,然后将它们收集到一个新列表中,然后创建一个字符串。下面是函数。

I have a function that creates an int stream, finds the distinct characters, sorts them and then collects them into a new list and then creates a string. Below is the function.

public static String longest(String s1, String s2) {
    String s = s1 + s2;
    return s.chars()
            .distinct()
            .sorted()
            .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
            .toString();
}

我真的很难弄清楚StringBuilder的收集是如何工作的,我在网上搜索过Java文档,但对它没有任何意义。从我可以看出,它创建了一个StringBuilder的新实例,只是在流中附加每个字符,任何人都可以给出更好的解释吗?谢谢

I am really struggling to work out how the collect with the StringBuilder is working, I have searched online and the Java docs but can't make any sense of it. From what I can make out, it creates a new instance of StringBuilder and just appends each character in the stream, can anyone give a better explanation? Thank you

推荐答案

参数1:创建起始结果(在这种情况下,你的新 StringBuilder )。

Argument 1: Creates your starting result (in this case, your new StringBuilder).

参数2:在结果中添加一个元素( String )( StringBuilder )。

Argument 2: Adds an element (String) to your result (StringBuilder).

参数3:如果并行运行流,则多个 StringBuilders 将被创建。这是为了将这些组合在一起。

Argument 3: If you run the stream in parallel, multiple StringBuilders will be created. This is for combining these together.

这篇关于Java 8 Int Stream使用StringBuilder进行收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆