The purpose of using Map is to map the keys to values.
The Map interface includes methods for basic operations (put, get, remove, containsKey, containsValue, size etc), bulk operations ( putAll and clear), and collection views (keySet, entrySet, and values).
The Java platform contains three general-purpose Map implementations:
TreeMap
LinkedHashMap.
The following one-liners create a new HashMap, TreeMap and LinkedHasMap.
Map<K, V> sampleMap = new HashMap<K, V>;
Map<K, V> sampleMap = new TreeMap<K, V>;
Map<K, V> sampleMap = new LinkedHashMap<K, V>;
Suppose you want to Group Student By Department, then we can use the Map interface. Assume that you already have a list of students,
// Group students by department
Map<Department, List<Student>> GrpByDept = students.stream()
.collect(Collectors.groupingBy(Student::getDepartment));
The basic operations of Map include put, get, containsKey, containsValue, size, isEmpty etc. Let us assume, we have a Map that stores id and name as key and value respectively.(Refer to the code below)
We are using the put(K, V) method to insert the key and value to the Map.
The isEmpty() returns a boolean - 'true' if Map is empty, 'false' if Map is not empty.
The size of the Map can be obtained by the size() method.
The containsKey() method provides a check if the passed key is present inside the map or not.
The containsValue() method provides a check if the passed value is present inside the map or not.
import java.util.*;
public class MapInterfaceDemo {
public static void main(String[] args) {
Map<Integer, String> sampleMap = new HashMap<Integer, String>();
// Inserting Data into the Map using put().
sampleMap.put(1, "Smith");
sampleMap.put(2, "Jonas");
sampleMap.put(3, "Doramy");
// Checking whether map is empty or not
boolean isEmptyCheck = sampleMap.isEmpty();
System.out.println("Empty Map :: " + isEmptyCheck);
// Printing the Size of the Map
System.out.println(sampleMap.size() + " is the size of the Map.");
// Printing the Map
System.out.println("Printing the Map :: " + sampleMap);
// Checking whether Map Contains a specific key using the containsKey method
if (sampleMap.containsKey(1)) {
System.out.println("Map contains the passed key.");
}
// Checking whether Map Contains a specific key using the containsValue method
if (sampleMap.containsValue("Jonas")) {
System.out.println("Map contains the passed value.");
}
}
}
The Collection view methods allow a Map to be viewed as a Collection in these three ways:
keySet — the Set of keys contained in the Map.
for (KeyType key : sampleMap.keySet())
System.out.println(key);
values — The Collection of values contained in the Map. This Collection is not a Set, because multiple keys can map to the same value.
entrySet — the Set of key-value pairs contained in the Map. The Map interface provides a small nested interface called Map.Entry, the type of the elements in this Set.
for (Map.Entry<KeyType, ValType> entry : sampleMap.entrySet())
System.out.println(entry.getKey() + ": " + entry.getValue());
A multimap is a Map that can map each key to multiple values. It is equivalent to using a Map whose values are List instances. The Java Collections Framework doesn't include an interface for multimaps because they aren't used all that commonly.
This technique is implemented in this way👇
Map<KeyType, List<ValType>> m = new HashMap<KeyType, List<ValType>>();