Java - Map Interface and Related Classes
Map Interface
The Map interface is a Collection that stores and manages data as key-value pairs by assigning another object as the key value. It does not allow duplicate key objects, but it does allow duplicate data. Because it does not manage indexes, it is not suitable for tasks such as sorting. Subclasses that implement the Map interface include HashMap, LinkedHashMap, and TreeMap.
- Map
- HashMap
- LinkedHashMap
- SortedMap
- TreeMap
- HashMap
Map Methods
| Method | Description |
|---|---|
void clear() |
Deletes the objects stored in the Map. |
boolean containsKey(Object key) |
Returns whether the specified key exists. |
boolean containsValue(Object value) |
Returns whether the specified value exists. |
V get(Object key) |
Returns the data for the specified key. |
boolean isEmpty() |
Returns whether the Map is empty. |
Set<K> keySet() |
Returns the list of keys as a Set object. |
V put(K key, V value) |
Adds new data made up of a key and value. |
void putAll(Map<? extends K, ? extends V> m) |
Adds another Map to the Map. |
V remove(Object key) |
Deletes the data(value) for the specified key and returns the deleted data(value). |
int size() |
Returns the number of elements in the Map. |
Collection<V> values() |
Returns the list of values as a Collection. |
V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) |
In Java 8, this method can be used to write shorter code for the pattern that retrieves a value when the value for a key exists and creates one when it does not. |
computeIfAbsent Method
Absent means not present. This method puts a Value when the corresponding Key value is not found in the Map.
The following example checks whether a value for a specific key exists in a Map, and inserts a new value if it does not.
Map<Key, Value> map = new HashMap();
Value value = map.get(key);
if (value == null) {
value = getNewValue(key);
map.put(key, value);
}
The example above can be written more concisely with the computeIfAbsent method as follows.
Map<Key, Value> map = new HashMap();
Value value = map.computeIfAbsent(key, k -> getNewValue(key));
HashMap Class
HashMap is a Map-based Collection that uses a hash table to store data. When storing data, HashMap specifies an object corresponding to the key value in addition to the element to be stored, and when retrieving data, it retrieves the data by the key value that was stored. Therefore, when creating a HashMap object, you can also use a constructor that specifies the key value and value. HashMap does not support sorting.
HashMap Example
package com.devkuma.tutorial.java.util.collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class HashMapClass1 {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
System.out.println("\n-- Example1 -----");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("Key=" + entry.getKey() + ", Value=" + entry.getValue());
}
System.out.println("\n-- Example2 -----");
Iterator<Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
System.out.println("Key : " + entry.getKey() + " Value :" + entry.getValue());
}
System.out.println("\n-- Example3 -----");
// Java8 only, forEach and Lambda
map.forEach((k, v) -> System.out.println("Key=" + k + ", Value=" + v));
System.out.println("\n-- Example4 -----");
for (Integer key : map.keySet()) {
System.out.println("key=" + key + ", value=" + map.get(key));
}
}
}
The execution result is as follows.
-- Example1 -----
Key=1, Value=one
Key=2, Value=two
Key=3, Value=three
-- Example2 -----
Key : 1 Value :one
Key : 2 Value :two
Key : 3 Value :three
-- Example3 -----
Key=1, Value=one
Key=2, Value=two
Key=3, Value=three
-- Example4 -----
key=1, value=one
key=2, value=two
key=3, value=three
LinkedHashMap Class
LinkedHashMap is a class that extends HashMap and is a Collection that stores data in the order it was entered. LinkedHashMap stores data as key-value pairs, and when data is retrieved, it is retrieved in the order it was entered. Unlike HashMap, it guarantees ordering by insertion order.
LinkedHashMap Example
package com.devkuma.tutorial.java.util.collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapClass {
public static void main(String[] args) {
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
map.put("11", 104);
map.put("23", 133);
map.put("32", 111);
map.put("24", 123);
map.put("15", 144);
map.put("36", 113);
for (String key : map.keySet()) {
System.out.println("key=" + key + ", value=" + map.get(key));
}
}
}
The execution result is as follows.
key=11, value=104
key=23, value=133
key=32, value=111
key=24, value=123
key=15, value=144
key=36, value=113
TreeMap Class
TreeMap is a class that implements the Map interface and the SortedMap interface, and it is a Map-type Collection that supports sorting. TreeMap stores data as key-value pairs, supports sorting, and also retrieves data quickly. Unlike HashMap, it stores key values in ascending order.
TreeMap Example
package com.devkuma.tutorial.java.util.collection;
import java.util.Map;
import java.util.TreeMap;
public class TreeMapClass {
public static void main(String[] args) {
Map<String, Integer> map = new TreeMap<String, Integer>();
map.put("11", 104);
map.put("23", 133);
map.put("32", 111);
map.put("24", 123);
map.put("15", 144);
map.put("36", 113);
for (String key : map.keySet()) {
System.out.println("key=" + key + ", value=" + map.get(key));
}
}
}
The execution result is as follows.
key=11, value=104
key=15, value=144
key=23, value=133
key=24, value=123
key=32, value=111
key=36, value=113