partitioningBy splits elements into a Map<Boolean, List> of matches and non-matches.
Map<Boolean, List<Integer>> parts = IntStream.rangeClosed(1, 8).boxed()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));
System.out.println("even: " + parts.get(true));
System.out.println("odd: " + parts.get(false));
even: [2, 4, 6, 8]
odd: [1, 3, 5, 7]
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-26