• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt on the working and performance of HashMap.  RSS feed

 
Ambuj Jauhari
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have some confusion with respect to the working of Hashmap.

What is know about the working of a hashmap is that when a hashmap is created a array of linked list is created i.e. each element that is added to the hashmap the hashcode is calculated of the key object that hashcode is the index in the array at which the element would be added to the linked list.

My doubt here is that suppose i have some code where i am adding 2 objets to a hashmap one hashmap returns returns the hashcode 1 and the element will be added to the linked list at index - 1. Now if i am adding another element whose hashcode returned is suppose INT.max_value or very large number so will the array of linked list be of size this much (INT.max_value or very large number) ??
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I am afraid you have posted in the wrong location. I am sure Henry who moderates this forum won’t mind if I move your post somewhere more appropriate.
The array in a HashMap defaults to 16 length; this discussion is about how the hash codes are converted to array indices. If you need to know any more, be sure to ask.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, EaseUp (←click on the link). Putting "Urgent" in your subject line does not usually get it the kind of attention you want. We're all volunteers here and if anyone wants to reply, they will reply in their own time. I have edited the subject and removed "Urgent".
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now if i am adding another element whose hashcode returned is suppose INT.max_value or very large number so will the array of linked list be of size this much (INT.max_value or very large number) ??


You have the wrong idea, hashcodes are just the start of getting the index. you should study the ACTUAL code in HashMap instead of speculating. One of the great things about Java is that the standard library code is published. Here for example is HashMap put method:



Use the Source!

Bill

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!