00:00

Problem Statement

Input/Output Examples

Visual Explanation

The predicate is evaluated once per element. Unlike groupingBy(), partitioningBy() always creates both keys, so code that reads get(true) never gets null.

Approaches

The collector does the split in one expression. The loop shows what it does underneath.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the size of the list. Both approaches test each element once and store every element in one of the two lists.

Interview Tips and Recommended Approach

  • Know the difference from groupingBy(). groupingBy() only creates keys that occur; partitioningBy() always has both.
  • Test for even, not odd. -3 % 2 is -1, so n % 2 == 1 misclassifies negative odd numbers.
  • Add a downstream collector for counts or sums, for example partitioningBy(test, counting()).
  • Use it for any yes/no split: passed and failed scores, active and inactive users, and so on.

Recommended Approach: Collectors.partitioningBy()

Why: it expresses a two-way split in one readable call and guarantees that both groups exist.