• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap and hashCode  RSS feed

 
Roopesh Rana
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have newly created HashMap, this is empty Map, now I want to insert one key value pare in this.

My Employee class return hashCode - 255.

My HashMap size is 0 to 15 currently.

so i want to know, how HashMap will handle it.


Regards
 
Mark Spencers
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read HashMap documentation (https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html):
Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that documentation actually says nothing about that.

Hash tables usually have an internal hash function that will map the hash code of your object to a bucket index.

I suggest you read this article: https://en.wikipedia.org/wiki/Hash_table
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!