xp
xp copied to clipboard
add collecting methods to aggregating classes
Every aggregating class (Nodes, Contents, etc. ) is quite often collected from streams in an inconvenient way:
Nodes.from( collection.stream()...collect( Collectors.toList() ) );
There is a way to improve them with an util method:
public static Collector<Node, Builder, Nodes> collecting()
{
return Collector.of( Builder::new, Builder::add, ( left, right ) -> left.addAll( right.build() ), Builder::build );
}
As a result:
collection.stream()...collect( Nodes.collecting() );