I was trying to convert this
String r = "";
for ( Persona p : list ) {
r += p.lastName;
}
To stream().filter.collect() form, but I want to know how to write the collect with a lambda expression (not method references). I couldn't find a good example.
This is what I have
class B {
public static void main( String ... args ) {
List<Person> p = Arrays.asList(
new Person("John", "Wilson"),
new Person("Scott", "Anderson"),
new Person("Bruce", "Kent"));
String r;
String s = p.stream()
.filter( p -> p.lastName.equals("kent"))
.collect((r, p) -> r += p.lastName);/// ?????
}
}
class Person {
String name;
String lastName;
public Person( String name, String lastName ) {
this.name = name;
this.lastName = lastName;
}
}
All the examples I find are using method references whereas I think there should be an easy way to write a lambda expression instead.
Collectors.joining()or is the question about implementing it yourself?.collect( (acc, p ) -> acc += p.lastName);map(person -> person.lastName)and then you can collect those Strings with.collect(joining()).Collector.ofor usingcollect(Supplier, BiConsumer, BiConsumer).