简体   繁体   English

Java 8 Int Stream使用StringBuilder进行收集

[英]Java 8 Int Stream collect with StringBuilder

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. 我有一个函数,它创建一个int流,找到不同的字符,对它们进行排序,然后将它们收集到一个新的列表中,然后创建一个字符串。 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();
}

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. 我真的很难弄清楚StringBuilder的收集是如何工作的,我已经在线搜索了Java文档,但是对它没有任何意义。 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? 从我可以看出,它创建了一个StringBuilder的新实例,只是在流中附加每个字符,任何人都可以给出更好的解释吗? Thank you 谢谢

To understand the three arguments, you need to understand what the stream needs to do: it loops through characters, and must append them to a StringBuilder. 要理解这三个参数,您需要了解流需要做什么:它遍历字符,并且必须将它们附加到StringBuilder。

So the first thing it needs to know is how to create an empty StringBuilder. 所以它需要知道的第一件事是如何创建一个空的StringBuilder。 That's what the first argument is for: it provides a function which, when called by the stream, creates an empty StringBuilder. 这就是第一个参数的用途:它提供了一个函数,当由流调用时,它创建一个空的StringBuilder。

The second thing it needs to know is what to do with each character in the stream. 它需要知道的第二件事是如何处理流中的每个字符。 It must append them to the StringBuilder. 它必须将它们附加到StringBuilder。 That's what the second argument is for: it's a function which, when called by the stream, appends the character to the StringBuilder. 这就是第二个参数的用途:它是一个函数,当由流调用时,将该字符附加到StringBuilder。

That's all you need if the stream is sequential. 如果流是顺序的,那就是你所需要的。 But if the stream is parallel, the stream splits the elements in several parts, and processes each part in parallel. 但是如果流是并行的,则流将元素分成几个部分,并且并行处理每个部分。 Let's say it just uses two parts. 让我们说它只使用两部分。 It calls the first function twice to create two empty StringBuilders, and it processes each part in parallel by using the second function to append characters to the two StringBuilders. 它调用第一个函数两次来创建两个空StringBuilder,它通过使用第二个函数将字符附加到两个StringBuilder来并行处理每个部分。

In the end, each part is transformed to a StringBuilder containing half of the characters. 最后,每个部分都转换为包含一半字符的StringBuilder。 So the stream needs to know how to combine those two StringBuilders together. 因此,流需要知道如何将这两个StringBuilder组合在一起。 That's what the third argument is for. 这就是第三个论点。 It's a function which, when called by the Stream, combines the two StringBuilder together by appending all the characters from the second one to the first one. 它是一个函数,当由Stream调用时,通过将第二个字符串中的所有字符附加到第一个字符串,将两个StringBuilder组合在一起。

Argument 1: Creates your starting result (in this case, your new StringBuilder ). 参数1:创建起始结果(在本例中为新的StringBuilder )。

Argument 2: Adds an element ( String ) to your result ( StringBuilder ). 参数2:向结果( StringBuilder )添加元素( String )。

Argument 3: If you run the stream in parallel, multiple StringBuilders will be created. 参数3:如果并行运行流,将创建多个StringBuilders This is for combining these together. 这是为了将这些组合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM