Map Interface in Java

Last Updated : 26 Nov, 2026

The Map interface in Java belongs to the  java.util package and stores data as key-value pairs. Each key must be unique, but multiple keys can have the same value.

  • Allows efficient retrieval and modification of values using keys.
  • Ordering, null support and performance depend on the implementation.
Java
import java.util.HashMap;
import java.util.Map;
class Geeks{
    public static void main(String[] args){
        Map<String, Integer> marks = new HashMap<>();

        // Add key-value pairs
        marks.put("Java", 90);
        marks.put("Python", 85);
        marks.put("C++", 80);

        // Retrieve value using a key
        System.out.println("Java Marks: " + marks.get("Java"));
        System.out.println("Total Subjects: " + marks.size());
    }
}

Output
Java Marks: 90
Total Subjects: 3

Explanation: The HashMap stores subject names as keys and marks as values. The get() method retrieves the marks associated with the Java key, while size() returns the total number of mappings.

Declaration of the Map interface

public interface Map<K, V>

  • K represents the type of keys.
  • V represents the type of values.

Since Map is an interface, its objects cannot be created directly. Implementation classes such as HashMap and TreeMap are used to create Map instances.

Hierarchy of Map Interface

It is part of the Java Collections Framework, and its key implementation classes include HashMap, LinkedHashMap, TreeMap, and Hashtable.

map
Hierarchy of Map Interface

Java 21 introduced SequencedMap to represent maps with a defined encounter order. SortedMap extends SequencedMap, while NavigableMap extends SortedMap.

Implementation Classes of Map Interface

  • HashMap sores key-value pairs using hashing for fast access, insertion, and deletion.
  • LinkedHashMap similar to HashMap but maintains the insertion order of key-value pairs.
  • TreeMap stores key-value pairs in sorted order using natural ordering or a custom comparator.
  • Hashtable a synchronized Map implementation that doesn’t allow null keys or values.

Operations on Map Using HashMap

The HashMap class provides methods to perform common operations such as adding, updating, removing, and traversing entries.

1. Adding, Updating and Removing Entries

  • put() inserts a new key-value pair or replaces the value of an existing key.
  • remove() removes the mapping associated with a specified key.
Java
import java.util.HashMap;
import java.util.Map;
class Geeks{
    public static void main(String[] args){
        Map<Integer, String> map = new HashMap<>();

        // Add entries
        map.put(1, "Geeks");
        map.put(2, "Geeks");
        map.put(3, "Geeks");
        System.out.println("Initial Map: " + map);

        // Update an existing entry
        map.put(2, "For");
        System.out.println("Updated Map: " + map);

        // Remove an entry
        map.remove(3);
        System.out.println("After Removal: " + map);
    }
}

Output
Initial Map: {1=Geeks, 2=Geeks, 3=Geeks}
Updated Map: {1=Geeks, 2=For, 3=Geeks}
After Removal: {1=Geeks, 2=For}

Explanation: The put() method inserts three entries and updates the value associated with key 2. The remove() method then removes the entry with key 3.

2. Iterating Through a Map

The entrySet() method returns a set view of the mappings. A for-each loop can be used to access each key and its corresponding value.

Java
import java.util.HashMap;
import java.util.Map;
class Geeks{
    public static void main(String[] args){
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "Geeks");
        map.put(2, "For");
        map.put(3, "Geeks");

        // Traverse key-value pairs
        for (Map.Entry<Integer, String> entry : map.entrySet()){
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}

Output
1 : Geeks
2 : For
3 : Geeks

Explanation: The for-each loop traverses the entries returned by entrySet(). The getKey() and getValue() methods retrieve the key and value of each entry without requiring explicit type casting.

Note: HashMap does not guarantee iteration order. Therefore, the order of entries in the sample outputs may vary.

Methods of the Map Interface

Method

Description

containsKey(Object key)

Checks whether the specified key exists.

containsValue(Object value)

Checks whether the specified value exists.

keySet()

Returns a set view of all keys.

values()

Returns a collection view of all values.

isEmpty()

Checks whether the map contains no entries.

clear()

Removes all mappings.

putAll(Map m)

Copies mappings from another map.

getOrDefault(key, defaultValue)

Returns the mapped value or the default value if the key is absent.

putIfAbsent(key, value)

Associates a value with a key if it is absent or currently mapped to null.

merge(key, value, function)

Associates or combines a value with an existing mapping. Removes the mapping if the remapping function returns null.

Comment