Situation:
You want to split a list of names into two lists: one where the name length is greater than 3 and one where it’s not.
Solution:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
Map<Boolean, List<String>> partitionedNames = names.stream()
.collect(Collectors.partitioningBy(name -> name.length() > 3));
System.out.println(partitionedNames);
Explanation:partitioningBy
divides the stream into two groups based on the given predicate (name length greater than 3).
Result:
{false=[Bob], true=[Alice, Charlie, David]}
Leave a Reply