• 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

compare two Sets

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to compare Set<Custom-Object> with Set<String>.


Set<Custom-Object> set1 = foo();

set1 has sunday and monday (2 values)


Set<String> set2 = new HashSet<>();
set2.add("sunday");
set2.add("monday");


set1.equals(set2) returns false.

 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How on earth will a set of custom objects be equal to a set of Strings? Unless they are both empty.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, maybe OP didn't mean equal by the means of equals() method.
Maybe OP meant equal defined as: for every entry in custom-object-set (name it entry) check if there is entry.toString() in the string-set. Maybe.
We can not know for sure until OP clarifies the requirements.
 
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming Paweł's assumption, I would probably map-collect the first set to a new set of Strings, and then compare that to the second set.
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. If Paweł is correct about this new meaning for equality, that would work.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for the actual comparison, Set.containsAll() method might be helpful (given that you just want to see that all elements in one set is contained in another) or simply equals() method (I haven't tried it for a set)
However, as others have pointed out it does not make sense to compare two objects if they represent something else.

Its like saying i want to compare Set<Zombies> with Set<Zebras>
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic