WikiGalaxy

Personalize

Introduction to Java HashMap

What is a HashMap?

A HashMap in Java is a part of the Java Collection Framework and is used to store data in key-value pairs. It allows null values and the null key. It is not synchronized and is not ordered, meaning the order of the map is not guaranteed.


import java.util.HashMap;
class Example {
  public static void main(String args[]) {
      HashMap map = new HashMap<>();
      map.put("Apple", 1);
      map.put("Banana", 2);
      System.out.println(map);
  }
}
    

Console Output:

{Apple=1, Banana=2}

Adding Elements to HashMap

Using put() Method

The put() method is used to add elements to the HashMap. If the key already exists, the new value overwrites the old value.


import java.util.HashMap;
class Example {
  public static void main(String args[]) {
      HashMap map = new HashMap<>();
      map.put("Key1", "Value1");
      map.put("Key2", "Value2");
      map.put("Key1", "Value3"); // Overwrites Value1
      System.out.println(map);
  }
}
    

Console Output:

{Key1=Value3, Key2=Value2}

Removing Elements from HashMap

Using remove() Method

The remove() method removes the mapping for a key from this map if it is present.


import java.util.HashMap;
class Example {
  public static void main(String args[]) {
      HashMap map = new HashMap<>();
      map.put("One", 1);
      map.put("Two", 2);
      map.remove("One");
      System.out.println(map);
  }
}
    

Console Output:

{Two=2}

Iterating Over HashMap

Using for-each Loop

HashMap can be iterated using the for-each loop along with the entrySet() method, which returns a set view of the mappings contained in this map.


import java.util.HashMap;
import java.util.Map;
class Example {
  public static void main(String args[]) {
      HashMap map = new HashMap<>();
      map.put("A", "Apple");
      map.put("B", "Banana");
      for (Map.Entry entry : map.entrySet()) {
          System.out.println(entry.getKey() + " = " + entry.getValue());
      }
  }
}
    

Console Output:

A = Apple

B = Banana

Checking if HashMap is Empty

Using isEmpty() Method

The isEmpty() method returns true if the map contains no key-value mappings.


import java.util.HashMap;
class Example {
  public static void main(String args[]) {
      HashMap map = new HashMap<>();
      System.out.println("Is map empty? " + map.isEmpty());
      map.put("X", 10);
      System.out.println("Is map empty? " + map.isEmpty());
  }
}
    

Console Output:

Is map empty? true

Is map empty? false

Getting Size of HashMap

Using size() Method

The size() method returns the number of key-value mappings in the map.


import java.util.HashMap;
class Example {
  public static void main(String args[]) {
      HashMap map = new HashMap<>();
      map.put("C", "Cat");
      map.put("D", "Dog");
      System.out.println("Size of map: " + map.size());
  }
}
    

Console Output:

Size of map: 2

Replacing Values in HashMap

Using replace() Method

The replace() method replaces the entry for a specified key only if it is currently mapped to some value.


import java.util.HashMap;
class Example {
  public static void main(String args[]) {
      HashMap map = new HashMap<>();
      map.put("E", "Elephant");
      map.replace("E", "Eagle");
      System.out.println(map);
  }
}
    

Console Output:

{E=Eagle}

Checking for Key in HashMap

Using containsKey() Method

The containsKey() method returns true if this map contains a mapping for the specified key.


import java.util.HashMap;
class Example {
  public static void main(String args[]) {
      HashMap map = new HashMap<>();
      map.put("F", 6);
      System.out.println("Contains key 'F'? " + map.containsKey("F"));
      System.out.println("Contains key 'G'? " + map.containsKey("G"));
  }
}
    

Console Output:

Contains key 'F'? true

Contains key 'G'? false

Cloning a HashMap

Using clone() Method

The clone() method is used to create a shallow copy of the HashMap instance.


import java.util.HashMap;
class Example {
  public static void main(String args[]) {
      HashMap map1 = new HashMap<>();
      map1.put("G", "Giraffe");
      HashMap map2 = (HashMap) map1.clone();
      System.out.println("Original: " + map1);
      System.out.println("Cloned: " + map2);
  }
}
    

Console Output:

Original: {G=Giraffe}

Cloned: {G=Giraffe}

Merging Two HashMaps

Using putAll() Method

The putAll() method copies all of the mappings from the specified map to this map.


import java.util.HashMap;
class Example {
  public static void main(String args[]) {
      HashMap map1 = new HashMap<>();
      map1.put("H", "Horse");
      HashMap map2 = new HashMap<>();
      map2.put("I", "Iguana");
      map1.putAll(map2);
      System.out.println(map1);
  }
}
    

Console Output:

{H=Horse, I=Iguana}

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025