1

Have a List<SomeObject> where SomeObject has a date field among other fields. Using Java Streams was looking to obtain two lists, one containing the SomeObject instances for which the date field is non-empty while the other containing the remaining SomeObject instances with empty date fields.

Eugene
  • 110,516
  • 12
  • 173
  • 277
John C
  • 1,729
  • 4
  • 24
  • 42

1 Answers1

3

You are looking for Collectors.partitioningBy:

Map<Boolean,List<SomeObject>> partition =
    list.stream().collect(Collectors.partitioningBy(s->s.getDate()!=null));
Eran
  • 374,785
  • 51
  • 663
  • 734
  • What is variable s here? – John C Jul 17 '17 at 18:17
  • 1
    @JohnC It's the argument of the `test(SomeObject t)` method of the `Predicate` functional interface passed to `Collectors.partitioningBy`. You can name it whatever you like. – Eran Jul 17 '17 at 18:19