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.
Asked
Active
Viewed 72 times
1
1 Answers
3
You are looking for Collectors.partitioningBy
:
Map<Boolean,List<SomeObject>> partition =
list.stream().collect(Collectors.partitioningBy(s->s.getDate()!=null));

Eran
- 387,369
- 54
- 702
- 768
-
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