This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding HashMap

 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Any one ?.
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sha Jar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic