aspose file tools*
The moose likes Beginning Java and the fly likes Understanding HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding HashMap" Watch "Understanding HashMap" New topic
Author

Understanding HashMap

Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72
Hi Folks,

AFAIK, Hashmap will use Linear Chaining to store values if there are multiple keys that hash to the same hash code.

But when I do a toString of the HashMap, why doesn't it show these values ?. I am I missing some thing ?.

Here is the Code Snippet :



I am I missing some thing ?


Thanks Much
_SM
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11412
    
  16

From the API:

put

public V put(K key,
V value)

Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

But why are the entries in the HashTable a Linked List ?. I am a lil confused.

Any one ?.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Steven Rodeo wrote:
But why are the entries in the HashTable a Linked List ?. I am a lil confused.


It's a linked list for two reasons... (1) There isn't a bucket per hashcode. Many hashcodes share the same bucket. ie... it is possible to have more than one hashcode per bucket. (2) Just because two objects have the same hashcode, doesn't mean that they are equal. The list is required for two objects that hash to the same bucket -- but are not equal.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sha Jar
Greenhorn

Joined: Mar 02, 2010
Posts: 23
Steven Rodeo wrote:I am I missing some thing ?.


Maybe you've missed that what determines whether a key is already present in a HashMap is the equals method alone.

The hashCode method is used only as an aid to the HashMap in its efforts to store the entries efficient internally.

So equals and hashCode have two distinct and very different purposes. Equals guards the HashMap from multiple entries. HashCode helps the Hashmap with the internal organization.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Hi

It's very easy Steven as Fred said : If the map previously contained a mapping for the key, the old value is replaced. That's all ..


Thanks,
Sherif
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Remember, according to the hashcode contract, "equal" instances must return the same hashcode. But it is not required that unequal objects return different hashcodes.

So in the context of a hashmap: Yes, multiple keys can certainly share the same hashcode. But in a map, each key must be unique (meaning not equal).

For example...

put(A, X);
put(B, Y);

...will result in two separate entries if A and B are not equal, regardless of whether A and B have the same hashcode. On the other hand, if A and B are equal, then this will result in only one entry, because keys must be unique, so (B, Y) will replace (A, X).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding HashMap