Map Interface in Java

The Map interface in Java is part of the Java Collections Framework, but it is not a subtype of the Collection interface. Unlike collections, which are used to store single elements, a Map is used to store key-value pairs, where each key is unique, and each key maps to exactly one value.

Key Characteristics of the Map Interface:

  1. Key-Value Pairs: Each entry in a Map consists of a key and a corresponding value. Keys must be unique, but values can be duplicated.
  2. No Duplicates: The Map does not allow duplicate keys, but it allows duplicate values.
  3. Not Ordered: Depending on the implementation, the Map may or may not maintain the order of the keys.
  4. Insertion order is not followed.
  5. In Map, both keys and values are objects.
  6. Only one null value is allowed at keys side, but, any number of null values are allowed at values side.

Common Implementations of Map:

  • HashMap: A hash table-based implementation that does not guarantee the order of keys.
  • LinkedHashMap: A hash table and linked list implementation that maintains the insertion order.
  • TreeMap: A red-black tree-based implementation that maintains keys in a sorted order.
  • Hashtable: A legacy class, similar to HashMap but synchronized.
  • WeakHashMap: A map implementation that uses weak references for keys, allowing for more aggressive garbage collection.
  • IdentityHashMap: A map implementation that compares keys using reference equality (==) instead of equals().

Methods in the Map Interface

MethodDescription
  
int size()

Returns the number of key-value mappings in the map.

int size = map.size();
boolean isEmpty()

Returns true if the map contains no key-value mappings.

boolean empty = map.isEmpty();
V get(Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

V value = map.get(key);
V put(K key, V value)

Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

V previousValue = map.put(key, value);

NOTE: 

Return Type: V (the previous value associated with the key, or null if there was no mapping)

V remove(Object key)

Removes the mapping for a key from this map if it is present.

V removedValue = map.remove(key);

NOTE:

Return Type: V (the previous value associated with the key, or null if there was no mapping)

boolean remove(Object key, Object value)

Removes the entry for the specified key only if it is currently mapped to the specified value.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
boolean removed = map.remove("One", 1);
System.out.println(removed); // Output: true
System.out.println(map); // Output: {}
boolean containsKey(Object key)

Returns true if this map contains a mapping for the specified key.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
boolean hasKey = map.containsKey("One");
System.out.println(hasKey); // Output: true
boolean containsValue(Object value)

Returns true if this map maps one or more keys to the specified value.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
boolean hasValue = map.containsValue(1);
System.out.println(hasValue); // Output: true
void putAll(Map<K, V> m)

Copies all of the mappings from the specified map to this map.

map.putAll(anotherMap);
void clear()

Removes all of the mappings from this map.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.clear();
System.out.println(map); // Output: {}
Set<K> keySet()

Returns a Set view of the keys contained in this map.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
for (String key : map.keySet()) {
    System.out.println("Key: " + key);
}
Collection<V> values()

Returns a Collection view of the values contained in this map.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
for (Integer value : map.values()) {
    System.out.println("Value: " + value);
}
Set<Map.Entry<K, V>> entrySet()

Returns a Set view of the mappings contained in this map. Each element in the returned set is a Map.Entry Object.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
boolean equals(Object o)

Compares the specified object with the map for equality.

Map<String, Integer> map1 = new HashMap<>();
map1.put("One", 1);
Map<String, Integer> map2 = new HashMap<>();
map2.put("One", 1);
boolean isEqual = map1.equals(map2);
System.out.println(isEqual); // Output: true
V 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.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
Integer value = map.getOrDefault("Two", 0);
System.out.println(value); // Output: 0
V putIfAbsent(K key, V value)

If the specified key is not already associated with a value (or is mapped to null), associates it with the given value and returns null, else returns the current value.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.putIfAbsent("One", 2);
map.putIfAbsent("Two", 2);
System.out.println(map); // Output: {One=1, Two=2}
boolean replace(K key, V oldValue, V newValue)

Replaces the entry for the specified key only if currently mapped to the specified value.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
boolean replaced = map.replace("One", 1, 2);
System.out.println(replaced); // Output: true
System.out.println(map); // Output: {One=2}
V replace(K key, V value)

Replaces the entry for the specified key only if it is currently mapped to some value.

Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
Integer replacedValue = map.replace("One", 2);
Code Example
Output
Contains 'Apple': true
Value for 'Banana': 15
Keys: [Apple, Banana]
Values: [10, 15]
Entries:
Apple = 10
Banana = 15
Is map empty after clear: true