Course contentsShow
← Java
Lesson 1446 of 2,87032. Streams API: Intermediate OperationsFree lesson

Understanding distinct: Removing Duplicates from Streams

Learn how distinct() uses equals() and hashCode() to filter duplicate elements from a stream.

Understanding distinct: Removing Duplicates from Streams

What you'll learn: How the distinct() operation removes duplicate elements from a stream by relying on equals() and hashCode() methods.

What distinct Does

The distinct() intermediate operation filters out duplicate elements from a stream, returning only unique values. Think of it like passing your stream through a filter that remembers what it has already seen and blocks any repeats from passing through.

How distinct Determines Duplicates

Here's the crucial part: distinct() uses the equals() method to check if two elements are the same, and it uses hashCode() for efficient lookup (similar to how a HashSet works internally). This means:

  • For wrapper types like Integer or String, duplicates are detected by value
  • For custom objects, you must properly implement equals() and hashCode() or distinct() won't work as expected

Basic Usage

Stream.of(1, 2, 2, 3, 3, 3, 4)
    .distinct()
    .forEach(System.out::println);
// Output: 1, 2, 3, 4
Stream.of("apple", "banana", "apple", "cherry")
    .distinct()
    .forEach(System.out::println);
// Output: apple, banana, cherry

Important Characteristics

  • distinct() is stateful: it must remember all distinct elements seen so far
  • Order is preserved for ordered streams (first occurrence is kept)
  • For unordered streams (like from a HashSet), any occurrence may be kept
  • Memory usage grows with the number of distinct elements

Key Takeaway: The distinct() operation removes duplicates from a stream by using equals() to compare elements and hashCode() for efficient tracking, making it essential that custom objects properly implement both methods.