← Back to Core Java Course | Chapter 11: Collections | Lesson 6 of 17

Java Map Interface

The Map interface represents a collection of unique keys, each mapping to exactly one value, making it the go-to structure for looking up data by an identifier rather than a position.

What is the Map Interface?

The Map interface represents a collection of key-value pairs, where each key is unique and maps to exactly one value, making it the go-to structure whenever data needs to be looked up quickly by an identifier rather than a position.

Example: What is the Map Interface?

java
import java.util.Map;
import java.util.HashMap;
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> ages = new HashMap<>();
		ages.put("Alice", 30); // key maps to exactly one value
		System.out.println(ages.get("Alice"));
	}
}

Map Implementations

HashMap, LinkedHashMap, and TreeMap are the three standard implementations of Map: HashMap offers the fastest lookups with no ordering guarantee, LinkedHashMap preserves insertion order, and TreeMap keeps entries sorted by key.

Example: Map Implementations

java
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.TreeMap;
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> hash = new HashMap<>();
		Map<String, Integer> linked = new LinkedHashMap<>(); // preserves insertion order
		Map<String, Integer> tree = new TreeMap<>(); // sorted by key
		hash.put("a", 1);
		linked.put("a", 1);
		tree.put("a", 1);
		System.out.println(hash + " " + linked + " " + tree);
	}
}

Key-Value Pairs

A Map stores data as pairs, where put associates a key with a value and get retrieves that value later using the same key -- putting a new value under an existing key overwrites the old value rather than creating a second entry.

Example: Key-Value Pairs

java
import java.util.Map;
import java.util.HashMap;
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> ages = new HashMap<>();
		ages.put("Alice", 30);
		ages.put("Alice", 31); // overwrites the old value
		System.out.println(ages.get("Alice"));
	}
}

Common Map Methods

The Map interface defines methods like put, get, remove, containsKey, and isEmpty that behave consistently across every implementation, so switching between HashMap, TreeMap, and LinkedHashMap doesn't change how the map is used.

Example: Common Map Methods

java
import java.util.Map;
import java.util.HashMap;
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> ages = new HashMap<>();
		ages.put("Alice", 30);
		System.out.println(ages.containsKey("Alice"));
		System.out.println(ages.isEmpty());
		ages.remove("Alice");
		System.out.println(ages.isEmpty());
	}
}

Iterating a Map

A Map can be iterated in three ways: keySet returns just the keys, values returns just the values, and entrySet returns both together as Map.Entry objects, which is the most common way to loop over a map's full contents.

Example: Iterating a Map

java
import java.util.Map;
import java.util.HashMap;
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> ages = new HashMap<>();
		ages.put("Alice", 30);
		ages.put("Bob", 25);
		for (Map.Entry<String, Integer> entry : ages.entrySet()) {
			System.out.println(entry.getKey() + ": " + entry.getValue());
		}
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.