user3364192 user3364192 - 6 months ago 15
Java Question

Java 8 - ommiting tedious collect method

Java 8 stream api is very nice feature and I absolutely like it. One thing that get's on my nerves is that 90% of the time I want to have input as a collection and output as collections. The consequence is I have to call

stream()
and
collect()
method all the time:

collection.stream().filter(p->p.isCorrect()).collect(Collectors.toList());


Is there any java api that would let me skip the stream and directly operate on collections (like
linq
in c#?):

collection.filter(p->p.isCorrect)

Answer

If you want to operate on collections Guava's FluentIterable is a way to go!

Example (get id's of 10 first vip customers):

FluentIterable
       .from(customers)
       .filter(customer -> customer.isVIP())
       .transform(Client::getId)
       .limit(10);
Comments