• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing duplicate strings with distinct()

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!

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.

 
Bartender
Posts: 390
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are calling distinct() on a stream of Winner objects, which will only check if the objects are identical
 
Matthias Utrata
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any idea of using the constructor to distinct only the String?
 
Bartender
Posts: 3904
43
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !

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.

Further reading:
1) https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#distinct--
2) https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-
3) https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--
 
Matthias Utrata
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mikalai Zaikin It does not
 
Jesse Duncan
Bartender
Posts: 390
47
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are just trying to get a list of the names, you could get the names using map()

Something like this:
If not, and you want to change what makes Winners equal, you should look at what Mikalai Zaikin suggested.
 
Matthias Utrata
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That does not work sadly, because a List <String> is provided and the List<Winner> requires a Winner Object
 
Mikalai Zaikin
Bartender
Posts: 3904
43
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !

It depends on what you is your ultimate goal:

- 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):

https://stackoverflow.com/questions/23699371/java-8-distinct-by-property




 
Matthias Utrata
Ranch Hand
Posts: 107
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. It works now!
 
Jesse Duncan
Bartender
Posts: 390
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthias Utrata wrote:That does not work sadly, because a List <String> is provided and the List<Winner> requires a Winner Object


Sorry, my bad. Yes, that should be List<String> tdfwin. Glad that helped!
 
permaculture is largely about replacing oil with people. And one tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic