Introduction

 

The Java HashMap class executes the Map interface, which permits us to store key-value pairs where keys must be unique. If you attempt to insert the duplicate key, it will substitute the element of the corresponding key. It is simple to conduct operations using the key index, like updating, deleting, etc. The HashMap class is found in java.util package.

 

 

 

HashMap in Java

 

Since Java 1.2, HashMapK (V) has been a part of the Java collection. This class is found in java.util package. It supplies the fundamental execution of the Map interface in Java. It keeps the data in key-value pairs, and you can access them by an index of another type (e.g., an integer). One object is used as a key (index) to another object (value). It permits storage of the null keys as well, but there must be only one null key object, and there can be any number of null values. This class makes no promises as to the order of the map. To use this class and its methods, you ought to import the java.util.HashMap package or its superclass.

Java HashMap includes values based on the key. Java hashmaps have unique keys. Java HashMap may have one null key and multiple null values. Java HashMap is non-synchronized. Java HashMap maintains no order. The initial default capability of the Java HashMap class is 16 with a load factor of 0.75.

 

 

 

Our Learners Also Read: Java StringBuilder Class: Methods, Examples, And More

 

 

 

Java HashMap Example

 

Let us look at an easy example of using HashMap to store key-value pairs.

 

import java.util.*;  

public class HashMapExample1

{  

(String args []) public static void main

{  

HashMapInteger, String> map=new HashMapInteger, String>(); HashMapInteger, String> map=new HashMapInteger, String>();

   map.put(1,"Guava"); 

   map.put(2,"Pine Apple");    

   map.put(3,"Mango");   

   map.put(4,"Banana");   

       

   System.out.println("Iterating Hashmap...");  

map.entrySet() (Map.Entry m)

    System.out.println(m.getKey()+" "+m.getValue());    

   }  

}  

}  

 

 Output:-

 

Guava

Pine Apple

Mango

Banana

 

 

 

Important Features of Hashmap

 

To access a value, one must know its key. The name "hash map" comes from a technique called hashing. Hashing is the process of converting a large string to a small string that defines the same string. A shorter value benefits indexing and more rapid searches. HashSet also uses HashMap internally.

A few significant features of HashMap in Java are:-

 

  • HashMap is a portion of java.util package.
  • HashMap spreads an abstract class called AbstractMap, which also provides insufficient execution of the Map interface.
  • It also executes a clonable and serializable interface. K and V in the above definition describe key and value, respectively.
  • HashMap does not allow duplicate keys but permits duplicate values. That means a single key cannot include more than one value, but more than one key can have a single value.
  • HashMap permits a null key as well, but only once and numerous null values.
  • This class makes no promises as to the order of the map; in that respect, it does not confirm that the order will remain consistent over time. It is approximately similar to a hash table but is unsynchronized.

 

 

 

Java Linked HashMap

 

The Linked HashMap class in Java is just like HashMap with the added characteristic of preserving the order of elements inserted into it. HashMap provided the benefit of quick insertion, search, and deletion, but it never held the track and order of insertion, which the Linked HashMap provides so that the elements can be accessed in their insertion order.

 

 

 

Important Features of a Linked Hashmap are Listed as Follows:-

 

  • A linked hash map includes values based on the key. It executes the Map interface and expands the HashMap class.
  • It has unique elements.
  • It may have one null key and numerous null values.
  • It is not synchronized.
  • It is similar to HashMap with an added feature that preserves insertion order. For instance, when we execute the code with a hash map, we get a distinct order of elements.

 

 

 

Conclusion

 

HashMap in Java is like the heritage Hashtable class, but it's not synchronized. It lets us store the null elements if there should be only one null key. HashMapK, V> has been used since Java 5, where K is the key and V is the value. It inherits the AbstractMap class and executes the Map interface.

 

full stack java developer course