This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

compare two Sets  RSS feed

 
Ranch Hand
Posts: 1490
  • Mark post as helpful
  • send pies
  • 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: 59086
180
  • Mark post as helpful
  • send pies
  • 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: 2164
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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: 8883
166
  • Mark post as helpful
  • send pies
  • 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: 59086
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree. If Paweł is correct about this new meaning for equality, that would work.
 
Bartender
Posts: 1803
45
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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>
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!