In Java HashMap is a very important and most used concept. It is mostly used in enterprise-level coding to mapping. Also, it is used in e-commerce applications to map orders with customers.

Map is a class of collection in Java which is used to store data in Key & Value pairs. In this tutorial we will learn the implementation of HashMap in Java.

HashMap Program In Java

import java.util.HashMap;
import java.util.Map;
public class Main{
public static void main(String[] args){
HashMap map = new HashMap<>();
map.put(“a”, 10);
map.put(“b”, 30);
map.put(“c”, 20);
System.out.println(map);
if (map.containsKey(“mno”))
{
Integer a = map.get(“mno”);
System.out.println(“value for key \”mno\” is:- ” + a);
}

map.clear();
print(map);
}

public static void print(Map map){
if (map.isEmpty()){
System.out.println(“map is empty”);
}
else{
System.out.println(map);
}
}
}

Now lets see

What is HashMap and its features in details.

HashMap is a part of Java’s collection framework that provides the implementation of the Map interface. HashMaps stores the data in key-value pairs. Each key has some value to print the value we fetch values from the key of HashMap and then print it.

HashMap uses a Hashing technique to converting a larger String to a smaller and store it into key-value pair.

Various features of HashMap in Java.

  • Features of HashMap
  • HashMap is available in the util package of Java.
  • HashMap extends an abstract class AbstractMap.
  • We cannot store duplicate keys in HashMap but we can store duplicate values.
  • HashMap can have only one null key but can have multiple null values.
  • Performance of Java HashMap

Performance of HashMap Depends upon Intial Capacity and Load Factor:

Initial Capacity: Capacity means size of the buckets which is declared when HashMap instance is created.

Load Factor: The Load Factor allowed to do Rehashing. Rehashing is basically a process to increase the capacity of HashMap.

HashMap Constructors

  1. HashMap()
  2. HashMap(int initial capacity)
  3. HashMap(int initial capacity, float load factor)
  4. HashMap(Map map)