Java Map
Interface
A map stores values based on keys, forming key-
value pairs. Each pair is an entry. Maps have unique
keys.
Java Map Hierarchy
Interfaces Classes
Map and SortedMap HashMap, LinkedHashMap, TreeMap
Map Features
1 Unique Keys
No duplicate keys allowed.
2 Duplicate Values
Duplicate values are permitted.
3 Nulls
HashMap and LinkedHashMap allow null keys and values.
4 TreeMap
TreeMap doesn't allow null keys or values.
Map Traversal
Maps cannot be directly traversed. Convert to Set
using keySet() or entrySet() methods.
Map Implementations
HashMap
Implements Map, no order maintained.
LinkedHashMap
Implements Map, maintains insertion order.
TreeMap
Implements Map and SortedMap, maintains
ascending order.
Map Interface Methods
Method Description
put(Object key, Object value) Inserts an entry into the map.
putAll(Map map) Inserts the specified map into the map.
putIfAbsent(K key, V value) Inserts the specified value with the specified key only if it's not
already present.
remove(Object key) Deletes the entry for the specified key.
remove(Object key, Object value) Removes the specified values with the associated specified keys.
Map Interface Methods (cont.)
Method Description
keySet() Returns a Set view containing all the keys.
entrySet() Returns a Set view containing all the keys and values.
clear() Resets the map.
compute(K key, BiFunction remappingFunction) Computes a mapping for the specified key and its current
mapped value.
computeIfAbsent(K key, Function mappingFunction) Computes a value using the given mapping function if the key is
not associated with a value.
Map Interface Methods (cont.)
Method Description
computeIfPresent(K key, Computes a new mapping given
BiFunction remappingFunction) the key and its current mapped
value if the value for the specified
key is present and non-null.
containsValue(Object value) Returns true if some value equal
to the value exists within the map.
containsKey(Object key) Returns true if some key equal to
the key exists within the map.
equals(Object o) Compares the specified Object
with the Map.
forEach(BiConsumer action) Performs the given action for each
entry in the map.
Map Interface Methods (cont.)
Method Description
get(Object key) Returns the object that contains the value associated with the key.
getOrDefault(Object key, V defaultValue) Returns the value to which the specified key is mapped, or
defaultValue if the map contains no mapping for the key.
hashCode() Returns the hash code value for the Map.
isEmpty() Returns true if the map is empty.
merge(K key, V value, BiFunction remappingFunction) Associates the specified key with the given non-null value if the
key is not already associated with a value or is associated with
null.
Map Interface Methods (cont.)
Method Description
replace(K key, V value) Replaces the specified value for a
specified key.
replace(K key, V oldValue, V Replaces the old value with the
newValue) new value for a specified key.
replaceAll(BiFunction function) Replaces each entry's value with
the result of invoking the given
function on that entry.
values() Returns a collection view of the
values contained in the map.
size() Returns the number of entries in
the map.