Map Interface in java
The java collection framework has an interface Map that is available inside the java.util package. The Map interface is not a subtype of Collection interface.
🔔 The Map stores the elements as a pair of key and value.
🔔 The Map does not allows duplicate keys, but allows duplicate values.
🔔 In a Map, each key can map to at most one value only.
🔔 In a Map, the order of elements depends on specific implementations, e.g TreeMap and LinkedHashMap have predictable order, while HashMap does not.
The Map interface has the following child interfaces.
Interface | Description |
---|---|
Map | Maps unique key to value. |
Map.Entry | Describe an element in key and value pair in a map. Entry is sub interface of Map. |
SortedMap | It is a child of Map so that key are maintained in an ascending order. |
NavigableMap | It is a child of SortedMap to handle the retrienal of entries based on closest match searches. |
The Map interface has the following three classes.
Class | Description |
---|---|
HashMap | It implements the Map interface, but it doesn't maintain any order. |
LinkedHashMap | It implements the Map interface, it also extends HashMap class. It maintains the insertion order. |
TreeMap | It implements the Map and SortedMap interfaces. It maintains the ascending order. |
Map Interface methods
The Map interface contains methods for handling elements of a map. It has the following methods.
The insertion of a key, value pair is said to be an entry in the map terminology.
Method | Description |
---|---|
V put(Object key, Object value) | Inserts an entry in the map. |
void putAll(Map map) | Inserts the specified map into the invoking map. |
V putIfAbsent(K key, V value) | Inserts the specified value with the specified key in the map only if that key does not exist. |
Set keySet( ) | Returns a Set that contains all the keys of invoking Map. |
Collection values( ) | Returns a collection that contains all the values of invoking Map. |
Set<Map.Entry<K,V>> entrySet() | Returns a Set that contains all the keys and values of invoking Map. |
V get(Object key) | Returns the value associated with the specified key. |
V getOrDefault(Object key, V defaultValue) | Returns the value associated with the specified key, or defaultValue if the map does not contain the key. |
boolean containsValue(Object value) | Returns true if specified value found in the map, else return false. |
boolean containsKey(Object key) | Returns true if specified key found in the map, else return false. |
V replace(K key, V value) | Used to replace the specified value for the specified key. |
boolean replace(K key, V oldValue, V newValue) | Used to replaces the oldValue with the newValue for a specified key. |
void replaceAll(BiFunction function) | Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. |
V merge(K key, V value, BiFunction remappingFunction) | If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. |
V compute(K key, BiFunction remappingFunction) | Used to compute a mapping for the specified key and its current mapped value. |
V computeIfAbsent(K key, Function mappingFunction) | Used to compute its value using the given mapping function, if the specified key is not already associated with a value, and enters it into this map unless null. |
V computeIfPresent(K key, BiFunction remappingFunction) | Used to compute a new mapping given the key and its current mapped value if the value for the specified key is present and non-null. |
void forEach(BiConsumer action) | It performs the given action for each entry in the map until all entries have been processed or the action throws an exception. |
V remove(Object key) | Removes an entry for the specified key. |
boolean remove(Object key, Object value) | Removes the specified values with the associated specified keys from the map. |
void clear() | Removes all the entries from the map. |
boolean equals(Object o) | It is used to compare the specified Object with the Map. |
int hashCode() | Returns the hash code for invoking the Map. |
boolean isEmpty() | Returns true if the map is empty; otherwise returns false. |
int size() | Returns total number of entries in the invoking Map. |
In the upcoming tutorials, we will discuss in detail how the above methods used to handle the elements of collections like HashMap, LinkedHashMap, and TreeMap.