Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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: 332
42
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: 3699
38
  • 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
 
Jj Roberts
Bartender
Posts: 332
42
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: 3699
38
  • 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!
 
Jj Roberts
Bartender
Posts: 332
42
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!
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic