• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashmaps and HashSets  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
people use the analogy buckets when it comes to HashMaps and HashSets I'm not sure why but anyway
as you can see the output will be yes for .equals and false for ==,so what will happen when you add these to the set,for an object to be equal they need to have the same hashcode so even though .equals will return true for the two strings in the example, == will return false,so will both strings get added to different buckets? and if they do wouldn't that mean there will be a duplicate entry for "John"

thanks



 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashSet and HashMap use the equals() method. == is never used to compare two objects for equality, unless you're specifically interested in reference equality. This is what IdentityHashMap does.

Objects that have the same hash code may actually end up in different buckets. What buckets are exactly and how objects are distributed over them are advanced concepts and really just implementation details. All you need to know is that if you want to use a HashMap or HashSet reliably, you need to give equals() and hashCode() a proper implementation.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan much appreciated
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a quick question since we are on this subject

why do I get a stackOverflow exception I know the error in the code should be == not .equals() but how come that code will cause a stackoverflow?

thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code won't give you a StackOverflowError unless the method it's in is somewhere within a recursive method call. Can you post the entire method, and its calling method?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



thanks


 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your equals() method is causing the problem. At line 44, the equals() method will call the equals() method, which in turn calls the equals() method, which will call the equals() method, etc. Finally, the stack explodes.

you should not override equals() unless all of an object's properties play a part in making it unique. What if I have two objects called "Jupiter", but they have different orbital lengths. Are they equal, really? If the only property that makes an object unique is some kind of identifier (in this case, the name of the planet), you should not make them override equals(), and you should not put them in a Set. Instead, put them in a Map, using the name as the key:
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing, you can easily implement hashCode() using Objects.hash(). No need to perform tricky calculations.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Stephan much appreciated
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!