Last Updated: 20 April, 2023
In Java, a Map is an interface. It was introduced in the JDK 1.0 version and placed in the java.util package.
A Map is an object that stores a collection of elements in the form of key-value pairs. The Map does not allow duplicate keys but allows duplicate values. It maps keys to values and is designed for faster lookups. The name "map" is derived from the fact that each key maps to a value, and these key-value pairs are called map entries.
The Map interface is a member of the Java Collections Framework, but the Map interface is not a subtype of the Collection interface.
Here, using the Map interface's put() method, we are adding the key and value pair into the map where each key is bound to each value. A Map contains only unique keys, but values may be duplicated.
Let's see the complete hierarchy of Java Map interfae below.
Here, K is the key Object type and V is the value Object type.
Since Map is an interface, we cannot create objects, so Map is implemented by the below classes in Java:
The Map interface is also extended by the following sub-interfaces:
👉 Let's briefly know about each class's features:
Class | Added in Version | Syncronized | Key/Value | Fail-Fast | Mainten Insertion Order |
---|---|---|---|---|---|
HashMap | 1.2 | No | Only one null Key but Mutiple values can be null | Yes | No |
LinkedHashMap | 1.4 | No | Only one null Key but Mutiple values can be null | Yes | Yes |
HashTable | 1.0 | Yes | Does not allow null as a key or value | No | No |
TreeMap | 1.2 | No | cannot have null key but can have multiple null values | Yes | No |
WeakHashMap | 1.2 | No | Only one null Key but Mutiple values can be null | Yes | No |
IdentityHashMap | 1.4 | No | Only one null Key but Mutiple values can be null | Yes | No |
Output
Iterating Hashmap and printing Key and Value
in - India
en - England
us - United State
ca - Canada
Java Map interface has the following methods.
Method | Description |
---|---|
void clear() | Removes all key-value pairs from the map. |
public boolean containsKey(Object k) | Returns true if this map contains specified key otherwise returns false. |
public boolean containsValue(Object v) | Returns true if the map contains specified value otherwise returns false. |
public Set entrySet() | Returns a Set that contains the entries of this map. The set contains objects of type Map.Entry. It provides a set-view of this map. |
public Object get(Object k) | Returns the value associated with the specified key. |
public boolean isEmpty() | Returns true if this map is empty otherwise returns false. |
public Set keySet() | Returns a Set that contains the keys in this map. This method provides a set-view of the keys in this map. |
public Object put(Object k, Object v) | Puts an entry in this map, overwriting any previous value associated with the specified key. Returns previous value linked to the key. Return null if the key does not already exist. |
public void putAll(Map m) | Puts all the entries from m into this map. |
default V putIfAbsent(K key, V value) | It inserts the specified value with the specified key in the map only if it is not already specified. |
public Object remove(Object k) | Removes the entry whose key equals to specified key from this map. Returns the value to which this map previously associated with a specified key, or null if the map contained no mapping for the key. |
boolean remove(Object key, Object value) | Removes the specified values with the associated specified keys from the map. |
public int size() | Returns the number of entries in this map. |
public Collection values() | Returns a collection containing the values in this map. This method provides a collection-view of the values in this map. |
Entry is a nested interface of Map and it is declared as static so will be accessed by Map.Entry name. It returns a collection-view of the map, whose elements are of this class. It provides methods to get keys and values.
Java Map.Entry Interface has the following methods.
K getKey() | It is used to get a key. |
V getValue() | It is used to get a value. |
int hashCode() | It is used to get hashCode. |
V setValue(V value) | It will replace the value corresponding to this entry with the specified value. |
boolean equals(Object o) | It will compare the specified object with the other existing objects. |
static <K extends Comparable<? super K>,V> Comparator<Map.Entry<K,V>> comparingByKey() | It will get a comparator that compares the objects in natural order on key. |
static <K,V> Comparator<Map.Entry<K,V>> comparingByKey(Comparator<? super K> cmp) | It will get a comparator that compares the objects by key using the given Comparator. |
static <K,V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() | It will get a comparator that compares the objects in natural order on value. |
static <K,V> Comparator<Map.Entry<K,V>> comparingByValue(Comparator<? super V> cmp) | It will get a comparator that compares the objects by value using the given Comparator. |
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why doesn't Map extend the Collection Interface?
A Map is an object that stores a collection of elements in the form of key-value pairs. The reason that the Map doesn’t extend the Collection interface is that the add(E e) method of the Collection interface doesn’t support the key-value pair like the Map interface’s put(K, V) method.