Industry Ready Java Spring Boot, React & Gen AI — Live Course
JavaCollections

Map Interface

1. Introduction

The Map interface in Java is used to store data in key-value pairs. Unlike List and Set, a Map is not considered a direct subtype of Collection.

In a map:

  • Each key must be unique
  • Values may be duplicated
  • A key maps to exactly one value

Example:

import java.util.HashMap;
import java.util.Map;

public class Demo {
    public static void main(String[] args) {
        Map<Integer, String> students = new HashMap<>();

        students.put(101, "Navin");
        students.put(102, "Kiran");
        students.put(103, "Riya");

        System.out.println(students);
    }
}

2. Why Map Is Useful

Use Map when you want fast association between a key and its value.

Common examples:

  • Student ID to student name
  • Username to password hash
  • Product ID to price
  • Country code to country name

3. Important Features

  • Stores key-value pairs
  • Keys are unique
  • Values can repeat
  • Some implementations allow null
  • Different implementations have different ordering behavior

4. Common Map Implementations

4.1 HashMap

  • No guaranteed order
  • Fast average performance
  • Allows one null key and multiple null values

4.2 LinkedHashMap

  • Maintains insertion order
  • Slightly more overhead than HashMap

4.3 TreeMap

  • Stores keys in sorted order
  • Slower than HashMap
  • Useful when sorted keys are needed

4.4 Hashtable

  • Legacy class
  • Synchronized
  • Usually avoided in modern Java

5. Creating a Map

Map<String, Integer> marks = new HashMap<>();

This means:

  • Key type is String
  • Value type is Integer

6. Common Methods

6.1 put()

Adds or updates a key-value pair.

Map<String, Integer> marks = new HashMap<>();

marks.put("Math", 95);
marks.put("Science", 88);
marks.put("Math", 99);

If the key already exists, the old value is replaced.

6.2 get()

Retrieves value by key.

System.out.println(marks.get("Math")); // 99

6.3 remove()

marks.remove("Science");

6.4 containsKey() and containsValue()

System.out.println(marks.containsKey("Math"));
System.out.println(marks.containsValue(99));

6.5 size(), isEmpty(), clear()

System.out.println(marks.size());
System.out.println(marks.isEmpty());
marks.clear();

7. Iterating Over a Map

There are different ways to iterate.

7.1 Using keySet()

for (String subject : marks.keySet()) {
    System.out.println(subject + " -> " + marks.get(subject));
}

7.2 Using entrySet()

This is usually the most practical way.

for (Map.Entry<String, Integer> entry : marks.entrySet()) {
    System.out.println(entry.getKey() + " -> " + entry.getValue());
}

7.3 Using values()

for (Integer mark : marks.values()) {
    System.out.println(mark);
}

8. Duplicate Keys and Values

Duplicate keys are not allowed:

Map<Integer, String> map = new HashMap<>();
map.put(1, "A");
map.put(1, "B");

System.out.println(map);

Output:

{1=B}

The second value replaces the first one.

Duplicate values are allowed:

map.put(2, "B");

This is valid.

9. null in Map

Whether null is allowed depends on the implementation.

Example with HashMap:

Map<String, String> map = new HashMap<>();

map.put(null, "Admin");
map.put("theme", null);

System.out.println(map);

10. Map Is Not a Collection

This is an important interview and theory point.

Map is part of the Java Collections Framework, but it does not extend the Collection interface.

That is because a map stores pairs, not individual elements.

11. Practical Example

import java.util.HashMap;
import java.util.Map;

public class WordCounter {
    public static void main(String[] args) {
        String[] words = {"java", "python", "java", "c", "python", "java"};

        Map<String, Integer> frequency = new HashMap<>();

        for (String word : words) {
            frequency.put(word, frequency.getOrDefault(word, 0) + 1);
        }

        System.out.println(frequency);
    }
}

Output:

{python=2, c=1, java=3}

12. Summary

  • Map stores data in key-value pairs.
  • Keys are unique, but values may repeat.
  • It does not extend Collection.
  • Common implementations are HashMap, LinkedHashMap, and TreeMap.
  • Methods like put(), get(), remove(), and entrySet() are frequently used in real programs.

Written By: Shiva Srivastava

How is this guide?

Last updated on

Telusko Docs