This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding an element in a map

 
Neelesh Bodas
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand correctly, the process of searching a key in a map is a two step process
a) use hashcode() to get the correct bucket
b) use equals() method to find the object.
[ K&B, page 566, Chapter 7]

Now my question is : Is this just for our own understanding, or is it the way the keys are searched ACTUALLY while using the get() method?

I wrote a sample code which tries to search for a key in a TreeMap. I added print statements to hashcode() and equals() methods but none of them got printed. Hence the question.

The code is presented below for reference

 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand correctly, the process of searching a key in a map is a two step process
a) use hashcode() to get the correct bucket
b) use equals() method to find the object.
[ K&B, page 566, Chapter 7]

Now my question is : Is this just for our own understanding, or is it the way the keys are searched ACTUALLY while using the get() method?


Well not always. bucket concept applies to hashing collections, HashMap etc.

If you know the key element, then you can find value by first checking the presence of key by containsKey(Object key) method of Map or you can get a set of Map.Entry which will give you all keys and values of the Map.

Naseem
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic