![]() |
VOOZH | about |
In Java, the Map Interface is part of the java.util package and represents a collection of key-value pairs, where Keys should be unique, but values can be duplicated.
public interface Map<K, V>
Since a Map is an interface, we cannot create its object directly. We must use a class that implements it (e.g., HashMap).
Map<String, Integer> hm = new HashMap<>();
Map elements: {Geek3=3, Geek2=2, Geek1=1}
It is part of the Java Collections Framework, and its key implementation classes include HashMap, LinkedHashMap, TreeMap, and Hashtable.
Note: Map does not extend the Collection interface and is used to efficiently store and retrieve data using keys.
Now, let’s see how to perform a few frequently used operations on a Map using the widely used HashMap class.
Use the put() method to add elements to a Map. In HashMap, insertion order isn’t preserved, each element is stored based on its hash for faster access.
{1=Geeks, 2=For, 3=Geeks}
{1=Geeks, 2=For, 3=Geeks}
To update a value, use the put() method with the same key. The new value replaces the old one for that key.
Initial Map: {1=Geeks, 2=Geeks, 3=Geeks}
Updated Map: {1=Geeks, 2=For, 3=Geeks}
To remove an element from the Map, we can use the remove() method. This method takes the key value and removes the mapping for a key from this map if it is present in the map.
{1=Geeks, 2=For, 3=Geeks, 4=For}
{1=Geeks, 2=For, 3=Geeks}
There are multiple ways to iterate through the Map. The most famous way is to use a for-each loop and get the keys. The value of the key is found by using the getValue() method.
1 : Geeks 2 : For 3 : Geeks
| Methods | Action Performed |
|---|---|
| clear() | This method is used in Java Map Interface to clear and remove all of the elements or mappings from a specified Map collection. |
| containsKey(Object) | Checks if a key exists in the map. |
| containsValue(Object) | Checks if a value exists in the map. |
| entrySet() | Returns a set view of the map’s key-value pairs. |
| equals(Object) | Compares two maps for equality. |
| get(Object) | Returns the value for the given key, or null if not found. |
| hashCode() | This method is used in Map Interface to generate a hashCode for the given map containing keys and values. |
| isEmpty() | This method is used to check if a map is having any entry for key and value pairs. If no mapping exists, then this returns true. |
| keySet() | Returns a set view of the keys in the map. |
| put(Object, Object) | This method is used in Java Map Interface to associate the specified value with the specified key in this map. |
| putAll(Map) | This method is used in Map Interface in Java to copy all of the mappings from the specified map to this map. |
| remove(Object) | This method is used in Map Interface to remove the mapping for a key from this map if it is present in the map. |
| size() | This method is used to return the number of key/value pairs available in the map. |
| values() | Returns a collection view of the map’s values. |
| getOrDefault(Object key, V defaultValue) | Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key. |
| merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) | If the specified key is not already associated with a value or is associated with null, associate it with the given non-null value. |
| putIfAbsent(K key, V value) | Adds a mapping only if the key is not already mapped. |