Map and SortedMap
In Java, the Map and SortedMap interfaces are part of the Java Collections Framework. The Map
interface represents a collection of key-value pairs, where each key is unique, and each key maps to exactly one value. The SortedMap
interface extends Map
and guarantees that the keys are sorted in a specific order.
Map Interface
The Map interface defines a collection of key-value pairs. The key is used to retrieve its corresponding value. Common implementations of Map
include HashMap
, LinkedHashMap
, and TreeMap
.
Common Methods in Map Interface
put(K key, V value):
- Inserts the specified key-value pair into the map.
map.put("name", "Alice");
2. **get(Object key)**:
* Retrieves the value associated with the specified key.
```java
String value = map.get("name");
remove(Object key):
- Removes the key-value pair for the specified key.
map.remove("name");
containsKey(Object key):
- Checks if the map contains the specified key.
boolean hasName = map.containsKey("name");
containsValue(Object value):
- Checks if the map contains the specified value.
boolean hasValue = map.containsValue("Alice");
size():
- Returns the number of key-value pairs in the map.
int size = map.size();
keySet():
- Returns a set view of the keys contained in the map.
Set<K> keys = map.keySet();
values():
- Returns a collection view of the values contained in the map.
Collection<V> values = map.values();
SortedMap Interface
The SortedMap interface extends Map
and guarantees that its keys are sorted. It provides additional methods to navigate through the map based on key order.
Additional Methods in SortedMap
firstKey():
- Returns the first (lowest) key in the sorted map.
K firstKey = sortedMap.firstKey();
lastKey():
- Returns the last (highest) key in the sorted map.
K lastKey = sortedMap.lastKey();
headMap(K toKey):
- Returns a view of the portion of the map whose keys are less than
toKey
.
SortedMap<K, V> headMap = sortedMap.headMap("C");
- Returns a view of the portion of the map whose keys are less than
tailMap(K fromKey):
- Returns a view of the portion of the map whose keys are greater than or equal to
fromKey
.
SortedMap<K, V> tailMap = sortedMap.tailMap("B");
- Returns a view of the portion of the map whose keys are greater than or equal to
subMap(K fromKey, K toKey):
- Returns a view of the portion of the map whose keys range from
fromKey
totoKey
.
SortedMap<K, V> subMap = sortedMap.subMap("A", "D");
- Returns a view of the portion of the map whose keys range from
Example: Using Map and SortedMap
import java.util.*;
public class MapAndSortedMapExample {
public static void main(String[] args) {
// Using HashMap (Map implementation)
Map<String, Integer> map = new HashMap<>();
map.put("Alice", 30);
map.put("Bob", 25);
System.out.println("Value for Alice: " + map.get("Alice"));
System.out.println("Contains Bob: " + map.containsKey("Bob"));
// Using TreeMap (SortedMap implementation)
SortedMap<String, Integer> sortedMap = new TreeMap<>();
sortedMap.put("Alice", 30);
sortedMap.put("Bob", 25);
sortedMap.put("Charlie", 35);
System.out.println("First key: " + sortedMap.firstKey());
System.out.println("Last key: " + sortedMap.lastKey());
// Iterating through the sorted map
for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}