Java Map Interface
In this page:
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?
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"));
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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"));
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting