beginnerFunctional Programming
What does the Stream distinct() method do, and how does it determine uniqueness?
distinct() returns a stream with duplicate elements removed, determining uniqueness via each element's equals() (and, for hashing efficiency, hashCode()) method. For custom objects, this means distinct() only works correctly if equals()/hashCode() are properly overridden — otherwise it falls back to default object-identity comparison and won't remove logically-equal-but-distinct-instance duplicates.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response