Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java 8: Stream map&filter

Abdev2019Jul 7 2020 — edited Jul 7 2020

Hello JAVA,
I would like know if there is a new tips to manage collection-Stream specialy mpping and filtering data,
This is the story:
We have a large data, for e x a m p l e, list of 950.000 customers: List<Customer> customers= ArrayList()
we need only the name and email for example.
So we use stream like this : customers.stream().map(c->CustomerDTO(c.name, c.email)).collect(Collection.toList())
We have a conditions to filter customers,
If we did the basic methods-stream stream().filter().map()..., it's less efficient because of large data.
The idea is why not adding method that combine between map and filter like :

Predicat<Customer> myFilter = c->c.contribution>99;

stream().filterAndMap( myFilter, u->UserDTO(u.name, u.email) )

so w'll have something like that : if(myFilter) stream.add(mapper(u))

then we w'll avoid having double passing through all objects

Comments

Post Details

Added on Jul 7 2020
0 comments
258 views