Java Map Interface
In this page:
Map<KeyType, ValueType> map = new HashMap<>();
map.put(key, value);
map.get(key);
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 so it can be used by its short name
import java.util.Map;
// Import java.util.HashMap so it can be used by its short name
import java.util.HashMap;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new HashMap object and store it in ages
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
// Print a line to the console
System.out.println(ages.containsKey("Alice"));
// Print a line to the console
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 so it can be used by its short name
import java.util.Map;
// Import java.util.HashMap so it can be used by its short name
import java.util.HashMap;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new HashMap object and store it in ages
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
ages.put("Bob", 25);
for (Map.Entry<String, Integer> entry : ages.entrySet()) {
// Print a line to the console
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Calling
get()on a missing key and using thenullresult, which throwsNullPointerException. - Expecting duplicate keys to be kept, when
put()replaces the old value. - Using a mutable object as a key and changing it after insertion, so it can no longer be found.
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