• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking objects to see if same value  RSS feed

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I currently have two String objects I check to find out if they are the same value:


Now how would I check 10 objects to find out if any of them have the same value?

Please advise.
 
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
load them into a hashset. If the count of objects in the set doesn't match the number you added, you have a dup. Or more exactly, you have two or more with the same hashcode() which should be similar to the same equals()
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the hashcodes are the same, then equals() will be checked to see if the objects are really equal. A HashSet is perfectly capable of holding two objects with the same hash code that are not equal - it's just less efficient for lookup than when the objects have different hash codes.
 
Ed Carrington
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, any example links or tutorials on this I can view?
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
If the hashcodes are the same, then equals() will be checked to see if the objects are really equal. A HashSet is perfectly capable of holding two objects with the same hash code that are not equal - it's just less efficient for lookup than when the objects have different hash codes.


Oh yes, sorry. So you iterate over the collection, add to a hashset, and if the size doesn't grow, do a get() to find if it is really equal or not. Still lets you do it in O(n), altho with a large C
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Ed]: Thanks, any example links or tutorials on this I can view?

Googling "java hashset tutorial " or java hashset examples" gives a number of tutorials and examples. A good one to start with is Sun's Java tutorial on the Set interface.

[Pat]: So you iterate over the collection, add to a hashset, and if the size doesn't grow, do a get() to find if it is really equal or not.

I don't really understand what the get() would be for - you should have all the info you might want without that, I'd think. Note that the add() method has a return value which tells if the add() was successful - if it returns false, that's because there was a duplicate - which should only happen if the two objects are "really equal". Of course this only works if the ojects being added have valid equals() and hashCode() implementations. If they don't, there's really no way to use HashSet anyway.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:

I don't really understand what the get() would be for - you should have all the info you might want without that, I'd think. Note that the add() method has a return value which tells if the add() was successful - if it returns false, that's because there was a duplicate - which should only happen if the two objects are "really equal". Of course this only works if the ojects being added have valid equals() and hashCode() implementations. If they don't, there's really no way to use HashSet anyway.


Oh, I never used the return value to the add().

That is cleaner
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!