I'm testing the collect
method for streams. In this code I'm trying to build a single string from a stream of string by using a StringBuilder
in the first case and concatenation of String
in the second case. I am not understanding why the second case produces an empty string.
String[] strs = new String[] {"aaaaa", "bbbbbbb", "c", "dd"};
StringBuilder sb = Arrays.stream(strs).collect(StringBuilder::new, StringBuilder::append, StringBuilder::append);
System.out.println(sb);
String s = Arrays.stream(strs).collect(String::new, String::concat, String::concat);
System.out.println(s);