Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Int Stream collect with StringBuilder

Tags:

java

java-8

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();
}

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

like image 975
pocockn Avatar asked Dec 04 '16 19:12

pocockn


People also ask

Can you iterate through a StringBuilder?

A for-loop can iterate over the characters in a StringBuilder. We access the length() method to get the StringBuilder's size and then use charAt() to access chars. Length This method returns the count of characters in the StringBuilder.

Does Java 8 support streams?

Java 8 offers the possibility to create streams out of three primitive types: int, long and double. As Stream<T> is a generic interface, and there is no way to use primitives as a type parameter with generics, three new special interfaces were created: IntStream, LongStream, DoubleStream.

Which method can collect a Stream in a custom collection?

The toMap collector can be used to collect Stream elements into a Map instance.

Is StringBuilder a collection in Java?

StringBuilder is not part of the Collection s API nor does it extend from any of the classes from the API. If you did consider StringBuilder to be part of the collections API, you'd have to consider String and arrays as well :p. does it inherit from Collection ? What does "doesn't use Collections like ArrayList" mean?


1 Answers

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.

So the first thing it needs to know is how to create an empty StringBuilder. That's what the first argument is for: it provides a function which, when called by the stream, creates an empty 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. That's what the second argument is for: it's a function which, when called by the stream, appends the character to the 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.

In the end, each part is transformed to a StringBuilder containing half of the characters. So the stream needs to know how to combine those two StringBuilders together. 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.

like image 76
JB Nizet Avatar answered Sep 19 '22 14:09

JB Nizet