Forum Stats

  • 3,752,284 Users
  • 2,250,483 Discussions
  • 7,867,783 Comments

Discussions

Java 8: Stream map&filter

Abdev2019
Abdev2019 Member Posts: 1
edited Jul 7, 2020 12:30PM in Java Runtime Environment (JRE)

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