Course contentsShow
← Java
Lesson 1580 of 2,87034. Collectors in DepthFree lesson

The Map<Boolean, List<T>> Result Structure

Understand the two-key map structure: true and false keys, with each mapping to a list of elements matching that condition.

The Map<Boolean, List<T>> Result Structure

You'll learn how partitioningBy produces a two-key map where true and false each point to a list of matching elements.

What Makes partitioningBy Unique

When you use Collectors.partitioningBy(), the result is always a Map<Boolean, List<T>>. This is a special map structure with exactly two keys: true and false. Every element from your stream gets sorted into one of these two buckets based on whether it satisfies the predicate condition.

Think of it like a sorting machine with two output bins labeled "YES" and "NO." Every item that passes through must go into one bin or the other—there's no third option.

Understanding the Structure

The map has:

  • Key true: Maps to a List<T> containing all elements where the predicate returned true
  • Key false: Maps to a List<T> containing all elements where the predicate returned false

Both keys are always present in the resulting map, even if one of the lists is empty. This is different from regular groupingBy, where keys only exist if there are matching elements.

Why This Matters

This guaranteed two-key structure makes your code predictable and safe. You can always call result.get(true) or result.get(false) without worrying about null checks. An empty category simply returns an empty list, never null.

For example, partitioning students by passing grade (≥60%) produces a map where true holds all passing students and false holds all failing students. Both lists exist regardless of whether every student passed or every student failed.

Key Takeaway: partitioningBy always returns a Map<Boolean, List<T>> with exactly two keys (true and false), each mapping to a list of elements that matched or didn't match the condition.