I got a light issue with my program. I have a list and I wanna remove the multiple occurrances of the same string with .distinct(), but it does not work as expected. Maybe I got something wrong and I hope any of you can help me out.
The Stream.distinct() uses equals(Object o) method to eliminate duplicates. It's hard to tell if Winner class implements own equals(Object o), based on the code provided, so it's hard to tell why your code works not expecting.
- If you want just print names, use code proposed above by Jj:
- If you want to manipulate with distinct Winners later you need another approach because the map() operation will map from Winners to String and your Winners instances are lost.
In case you want to filter by distinct name and keep Winners in the stream, use a workaround described here (use distinctByKey custom predicate and filter stream by it):