| Author |
how hashCode() is used in HashMap?
|
ramu ta
Greenhorn
Joined: Feb 06, 2011
Posts: 18
|
|
|
how hashCode() is used in HashMap?
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
Check Jesper's post here.
|
[My Blog]
All roads lead to JavaRanch
|
 |
ramu ta
Greenhorn
Joined: Feb 06, 2011
Posts: 18
|
|
thnak you, i understood the usage of hashCode() in HashSet.
but i want to know the usage of hashCode() in HashMap.
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
The concept is the same. The hashCode is used to identify in which bucket the object will be place in.
|
 |
ramu ta
Greenhorn
Joined: Feb 06, 2011
Posts: 18
|
|
in HashSet, we can pass the searching object to the constructor of Hashset.
so the construtor is calling hashcode() method of searching object and going to the specific bucket for applying equals() method on those specific limited objects. this is clear to me...
but in HashMap.. we should pass key to get value... how this process is going? i m in confusion, will you please clear it?
|
 |
Vinoth Kumar Kannan
Ranch Hand
Joined: Aug 19, 2009
Posts: 276
|
|
veda kakarla wrote:......but in HashMap.. we should pass key to get value... how this process is going? i m in confusion, will you please clear it?
In a HashMap, essentially all the keys are stored in a Set. So, the same concept as for Set applies here too. When you ask for a value by giving the key, hashCode() and equals() shall be used to identify the bucket in which the key is in. Once the key is located, the value can be retrieved in no time.
|
OCPJP 6
|
 |
ramu ta
Greenhorn
Joined: Feb 06, 2011
Posts: 18
|
|
|
thank you so much
|
 |
 |
|
|
subject: how hashCode() is used in HashMap?
|
|
|