Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8 filter collect both type of value

Is there a way to collect both filtered and not filtered value in java 8 filter ?

One way is:

.filter( foo -> {
    if(!foo.apply()){
   // add to required collection
    }
    return foo.apply();
  }

Is there a better alternative ?

like image 687
Vivek Goel Avatar asked Mar 21 '26 07:03

Vivek Goel


1 Answers

 Map<Boolean, List<Foo>> map = 
     collection.stream().collect(Collectors.partitioningBy(foo -> foo.isBar());
like image 178
JB Nizet Avatar answered Mar 23 '26 21:03

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!