2) Simple HashMap

Req:
Need a data structure for fast searching operations.

Idea: 
Rather than linearly searching elements in worst case, create a hashed data structure.
DS: 
1) Create a Node (Key, Value , Hash , ptrNext)
2) Create a table of nodes, where each node location is a bucket

Put: (Addition of elements with (Key,Value) when key is not null)

1) Calculate index for table:
	1.1) Get hashcode 
	1.2) Perform hashing 
	1.3) Do bitwise and with size

2) Check if table[index] is null or not
	2.1) If null then put the value (create a new node) then ++size
	2.2) If not null (Hash Collision-> two diff objects having keys producing same hashcode hence same index)
 	     2.2.1) Check if the given key is already present or not (check both hash and equals) may 
		    be traversing linked list in the worst case.
		    if key is found then replace the old value with the new value.
	     2.2.2) Else create a new node with the given key,value pair and add to linked list then ++size
             (After certain threshold : linked list is converted to BST)

Get : (Get key,value mapping using key when key is not null)

1) Calculate index for table:
	1.1) Get hashcode 
	1.2) Perform hashing 
	1.3) Do bitwise and with size

2) Check if table[index] is null or not
	2.1) If null then return null
	2.2) If not null 
 	     2.2.1) Check if the given key is already present or not (check both hash and equals) may 
		    be traversing linked list in the worst case.
		    if key is found then return the node.
	     2.2.2) Else return null;        	   

Equality and Hash Code

While equality makes sense from a general perspective, hash codes are much more technical. If we were being a little hard on them, we could say that they are just an implementation detail to improve performance.

Most data structures use equals to check whether they contain an element. For example:

The variable contains is true because, while instances of "b" are not identical (again, ignoring String interning), they are equal.

Comparing every element with the instance given to contains is wasteful, though, and a whole class of data structures uses a more performant approach. Instead of comparing the requested instance with each element they contain, they use a shortcut that reduces the number of potentially equal instances and then only compare those.

This shortcut is the hash code, which can be seen as an object’s equality boiled down to an integer value. Instances with the same hash code are not necessarily equal but equal instances have the same hash code. (Or should have, we will discuss this shortly.) Such data structures are often named after this technique, recognizable by the Hash in their name, with HashMap the most notable representative.

This is how they generally work:

  • When an element is added, its hash code is used to compute the index in an internal array (called a bucket).

  • If other, non-equal elements have the same hash code, they end up in the same bucket and must be bundled together, e.g. by adding them to a list.

  • When an instance is given to contains, its hash code is used to compute the bucket. Only elements therein are compared to the instance.

This way, very few, ideally no equals comparisons are required to implement contains.

As equals, hashCode is defined on Object.

Hashing :

HashMap without hash() check, performance lag:

HashMap with hash() check,

With Interface :

Last updated

Was this helpful?