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.
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
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
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:-
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.
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.
About The Author:
Digital Marketing Course
₹ 29,499/-Included 18% GST
Buy Course₹ 41,299/-Included 18% GST
Buy Course