Java Map Interface
In Java, managing data that associates one item (key) with another (value) is essential in many programming tasks. The Map
interface in Java provides an efficient way to store and manipulate key-value pairs, where each key is unique, and each key is associated with exactly one value. The Map
interface is a part of the java.util
package and plays a critical role in many applications, from caching data to organizing user information.
The Map
interface represents a collection of key-value pairs. A map does not allow duplicate keys, and each key maps to exactly one value. Maps provide a convenient way to store and retrieve data, with efficient lookup capabilities based on the key.
Key features of the Map
interface:
Map
does not allow duplicate keys. If you insert a value for an existing key, the previous value is overwritten.null
key and multiple null
values, though some may vary (e.g., TreeMap
does not support null
keys).HashMap
.Java provides several classes that implement the Map
interface, each suited for different use cases:
HashMap:
Map
that stores key-value pairs in a hash table.HashMap<Integer, String> map = new HashMap<>();
TreeMap:
Map
implementation based on a Red-Black tree. It stores keys in sorted order.HashMap
.TreeMap<Integer, String> map = new TreeMap<>();
LinkedHashMap:
Map
implementation that maintains the order of insertion of keys.HashMap
due to overhead in maintaining the insertion order.LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
ConcurrentHashMap:
Map
suitable for concurrent access by multiple threads.ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
Here are some commonly used methods provided by the Map
interface:
put(K key, V value)
: Adds a key-value pair to the map. If the key already exists, it updates the value.
map.put("one", 1);
get(Object key)
: Retrieves the value associated with the specified key.
int value = map.get("one");
containsKey(Object key)
: Returns true
if the map contains the specified key.
boolean exists = map.containsKey("one");
containsValue(Object value)
: Returns true
if the map contains the specified value.
boolean exists = map.containsValue(1);
remove(Object key)
: Removes the key-value pair associated with the specified key.
map.remove("one");
size()
: Returns the number of key-value pairs in the map.
int size = map.size();
keySet()
: Returns a set view of the keys in the map.
Set<String> keys = map.keySet();
values()
: Returns a collection view of the values in the map.
Collection<Integer> values = map.values();
entrySet()
: Returns a set view of the key-value pairs in the map.
Set<Map.Entry<String, Integer>> entries = map.entrySet();
Here is an example of how to use a HashMap
to store and retrieve key-value pairs:
import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
public static void main(String[] args) {
// Create a HashMap
Map<String, Integer> map = new HashMap<>();
// Add key-value pairs
map.put("apple", 3);
map.put("banana", 2);
map.put("orange", 5);
// Retrieve a value using a key
System.out.println("Apple count: " + map.get("apple"));
// Check if a key exists
if (map.containsKey("banana")) {
System.out.println("Banana count: " + map.get("banana"));
}
// Iterate over the map entries
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}
Output:
Apple count: 3
Banana count: 2
apple -> 3
banana -> 2
orange -> 5
In this example:
put()
method is used to add key-value pairs to the map.get()
method retrieves the value associated with a key.containsKey()
method checks if a key exists.entrySet()
to iterate through the key-value pairs in the map.Here’s an example of how to use a TreeMap
, which keeps the keys sorted in natural order:
import java.util.Map;
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String[] args) {
// Create a TreeMap
Map<Integer, String> map = new TreeMap<>();
// Add key-value pairs
map.put(3, "Three");
map.put(1, "One");
map.put(2, "Two");
// Iterate over the map (keys will be in sorted order)
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}
Output:
1 -> One
2 -> Two
3 -> Three
Here, the keys are automatically sorted in ascending order, thanks to the TreeMap
.
Use a Map
when:
TreeMap
).ConcurrentHashMap
).