banner



How The Put Method Works In Java

Working of HashMap in Java


What is Hashing

It is the procedure of converting an object into an integer value. The integer value helps in indexing and faster searches.

What is HashMap

HashMap is a part of the Java collection framework. It uses a technique called Hashing. It implements the map interface. It stores the data in the pair of Cardinal and Value. HashMap contains an array of the nodes, and the node is represented equally a class. Information technology uses an array and LinkedList information structure internally for storing Key and Value. In that location are four fields in HashMap.

Working of HashMap in Java

Earlier agreement the internal working of HashMap, yous must be enlightened of hashCode() and equals() method.

  • equals(): Information technology checks the equality of two objects. Information technology compares the Key, whether they are equal or not. Information technology is a method of the Object course. It tin can exist overridden. If you override the equals() method, so it is mandatory to override the hashCode() method.
  • hashCode(): This is the method of the object course. It returns the retentiveness reference of the object in integer class. The value received from the method is used as the bucket number. The bucket number is the accost of the chemical element within the map. Hash code of null Fundamental is 0.
  • Buckets: Assortment of the node is chosen buckets. Each node has a information structure like a LinkedList. More than than 1 node can share the same saucepan. It may exist dissimilar in chapters.
Working of HashMap in Java

Insert Key, Value pair in HashMap

We use put() method to insert the Key and Value pair in the HashMap. The default size of HashMap is 16 (0 to fifteen).

Example

In the following example, we desire to insert three (Primal, Value) pair in the HashMap.

Let's meet at which index the Key, value pair will be saved into HashMap. When we phone call the put() method, and so it calculates the hash lawmaking of the Key "Aman." Suppose the hash code of "Aman" is 2657860. To store the Key in memory, we have to calculate the alphabetize.

Calculating Index

Index minimizes the size of the assortment. The Formula for computing the alphabetize is:

Where n is the size of the array. Hence the index value for "Aman" is:

The value 4 is the computed alphabetize value where the Central and value will store in HashMap.

Working of HashMap in Java

Hash Collision

This is the case when the calculated index value is the aforementioned for ii or more than Keys. Let's calculate the hash code for another Key "Sunny." Suppose the hash code for "Sunny" is 63281940. To shop the Fundamental in the memory, we have to summate index by using the alphabetize formula.

The value 4 is the computed index value where the Cardinal will be stored in HashMap. In this instance, equals() method check that both Keys are equal or non. If Keys are same, supervene upon the value with the electric current value. Otherwise, connect this node object to the existing node object through the LinkedList. Hence both Keys will be stored at alphabetize 4.

Working of HashMap in Java

Similarly, we will store the Primal "Ritesh." Suppose hash code for the Key is 2349873. The index value will be ane. Hence this Fundamental volition be stored at alphabetize one.

Working of HashMap in Java

become() method in HashMap

get() method is used to become the value by its Key. Information technology will not fetch the value if you don't know the Cardinal. When get(M Key) method is chosen, it calculates the hash code of the Key.

Suppose we have to fetch the Key "Aman." The post-obit method volition be chosen.

It generates the hash code every bit 2657860. Now calculate the alphabetize value of 2657860 by using alphabetize formula. The index value will exist 4, equally we have calculated to a higher place. get() method search for the index value 4. It compares the first chemical element Key with the given Key. If both keys are equal, then information technology returns the value else cheque for the adjacent chemical element in the node if it exists. In our scenario, it is found as the kickoff element of the node and render the value xix.

Let's fetch some other Key "Sunny."

The hash code of the Key "Sunny" is 63281940. The calculated alphabetize value of 63281940 is four, as we have calculated for put() method. Get to index iv of the assortment and compare the get-go element's Primal with the given Primal. It too compares Keys. In our scenario, the given Fundamental is the second element, and the next of the node is nothing. It compares the second element Central with the specified Fundamental and returns the value 29. Information technology returns naught if the adjacent of the node is null.


How The Put Method Works In Java,

Source: https://www.javatpoint.com/working-of-hashmap-in-java

Posted by: mccoyleir1945.blogspot.com

0 Response to "How The Put Method Works In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel