aspose file tools*
The moose likes Java in General and the fly likes Duplicate keys in Hashmap returns null get( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Duplicate keys in Hashmap returns null get("key")" Watch "Duplicate keys in Hashmap returns null get("key")" New topic
Author

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

Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270



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. ]


Time is what we want the most, but what we use the worst. -- William Penn
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Well for one, you are passing 0 to the get() method when the keys you entered into the map are StringBuffer objects.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

NB: what you have are duplicate keys not values.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

What are you expecting get(0) to do? Maybe you need to read the javadoc for the get() method again.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

They aren't really duplicate keys because they refer to two separate objects

Hunter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

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

Joined: Aug 16, 2005
Posts: 14150
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Duplicate keys in Hashmap returns null get("key")