Running the following stream example in Java8:
System.out.println(Stream
.of(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\")
.reduce(\"\", (s1,
To add to other answer,
You might want to use Mutable reduction, the doc specify that doing something like
String concatenated = strings.reduce("", String::concat)
Will give bad performance result.
We would get the desired result, and it would even work in parallel. However, we might not be happy about the performance! Such an implementation would do a great deal of string copying, and the run time would be O(n^2) in the number of characters. A more performant approach would be to accumulate the results into a StringBuilder, which is a mutable container for accumulating strings. We can use the same technique to parallelize mutable reduction as we do with ordinary reduction.
So you should use a StringBuilder instead.