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
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.
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.
The toMap collector can be used to collect Stream elements into a Map instance.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With