I'm studying Stream APIs in Java 8 and I'm facing a problem. The problem is I'm trying to understand the differences between ()->new StringBuilder
and StringBuilder::new
in the following code:
Stream<String> stream = Stream.of(new String[]{"b", "a", "s", "i", "l",});
StringBuilder basil = stream.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append);
System.out.println("basil");
and
Stream<String> stream = Stream.of(new String[]{"b", "a", "s", "i", "l",});
StringBuilder basil = stream.collect(()->new StringBuilder(), StringBuilder::append, StringBuilder::append);
System.out.println("basil");
When should I use the first syntax, and when should I choose the second one?
And why the above code accept StringBuilder::append
and doesn't accept StringBuilder::reverse