• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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

 
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. ]
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NB: what you have are duplicate keys not values.
 
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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 VI Editor Linux
  • 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
 
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!