Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Duplicate keys in Hashmap returns null get("key")

 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why does it return -> null 2...

I understand that the StringBuffer doesn't override the equals so it appears to be a different key.

can someone explain the get method ?




[Edit: Removed the typo as get(0), changed the subject to keys. ]
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well for one, you are passing 0 to the get() method when the keys you entered into the map are StringBuffer objects.

Hunter
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NB: what you have are duplicate keys not values.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you expecting get(0) to do? Maybe you need to read the javadoc for the get() method again.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They aren't really duplicate keys because they refer to two separate objects

Hunter
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer and StringBuilder do not override equals or hashCode. That means that equals uses only ==. In other words, your two keys are different, and that's why the map has 2 elements.

You try to get it using "1". Even if StringBuffer would have overridden equals to check the contents, you are trying to get the value using a String - an object from a completely different, incompatible class.

As for the get method, assuming the key is not null:
The hash method translates the key's actual hashCode, to "[defend] against poor quality hash functions" (quoted from its Javadoc). The indexFor method translates that in the bucket number. A bucket is a collection of objects which are grouped together. For HashMap that's based on the hash codes. The "table" field is the actual collection of buckets, as an Entry[] (with Entry being an inner class). This Entry class is actually a form of singly linked list, representing the bucket.
The for-loop checks each Entry to see if the key matches. If it does, that's the right Entry and its value is returned. If not the next Entry is checked until there are no more, in which case null is returned.
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob and all of you....



Its just that when you print an Hashmap it appears to be containing '1' and having duplicate keys (which is impossible).
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15483
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer is not suited to be used for the keys in a HashMap. The objects that you use for the keys should be immutable. If you use objects that are mutable and the hashcode of the object changes while it's in the map as a key, then the map will get confused and you can get strange things, such as seeing an entry when you iterate over the map, but when you get() it, it isn't there.

Don't use StringBuffer objects as keys in a HashMap.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, don't use any class for which equals hasn't been overridden (like StringBuffer), or you will only be able to use the original object to retrieve the value.

As for the printing containing "1" - it doesn't contain "1" as a String as you may think. When the StringBuffers they also print their current contents ("1") but they still are not Strings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic