In this article, we will discuss Map interface with all its important methods in detail
1. Misconception about Map interface:
- Often, Map interface is discussed under the umbrella of Collection framework
 - But unlike List, Set & Queue interface, Map interface doesn’t extends Collection interface
 - It is discussed, because it also deals with group of item, but items are key-value pairs or entries
 
2. Map interface:
- Map isn’t sub-interface of Collection interface (i.e.; Map has no relation with Collection interface)
 - To represent a group of key-value pairs as a single unit/entity, which accepts only unique keys and no restriction on values
 - Map allows only unique keys to be inserted but values can be duplicated
 - Here, both keys & values are objects of any type
 - Map interface is used to map values with key i.e.; we can retrieve values by querying map with its keys
 - Each key-value pair is known as Entry in Map, where Entry is a inner interface of Map interface (i.e.; Map interface contains Entry interface)
 - So, it is also referred as collection of entry objects
 - It is useful where there is frequent search/delete/update operation on the basis on key
 - Direct implementation classes for Map interface are HashMap, LinkedHashMap, WeakHashMap, IdentityHashMap and another one TreeMap (through SortedMap –> NavigableMap)
 - Map interface contains several methods to perform various operations like add, remove or check & verify, etc.
 - Present in java.util package
 
Source: Team BenchResources.Net
3. Map interface methods:
| Map method | Description | 
| Object put(Object key, Object value); | add/put a key-value pair (entry) in the invoking map
 
 if key already present, then returns old value of key if key isn’t present, then returns null;  | 
| void putAll(Map m); | add/put all entries from specified map into invoking map | 
| Object get(Object key); | returns value corresponding to specified key | 
| Object remove(Object key); | removes an key-value pair (entry) from invoking map for the specified key | 
| boolean containsKey(Object key); | returns true, if invoking map contains specified key
 
 otherwise returns false  | 
| boolean containsValue(Object value); | returns true, if invoking map contains specified value
 
 otherwise returns false  | 
| boolean isEmpty(); | returns true, if invoking map is empty
 
 otherwise returns false  | 
| int size(); | returns number of key-value pairs (entries) from invoking map | 
| void clear(); | removes all key-value pairs from invoking map | 
| Set keySet(); | returns set of keys from invoking map
 
 this provides collection/set views of Map  | 
| Collection values(); | returns collection containing the values of invoking map
 
 this provides collection/set views of Map  | 
| Set entrySet(): | returns set of map entries of type Map.Entry
 
 this provides collection/set views of Map  | 
4. Below listed classes implements Map interface:
5. Factors to consider while discussing any collection class:
We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,
- Underlying data structure
 - Duplicates are allowed or Not
 - Insertion order is maintained or Not
 - Whether NULL insertion is possible or Not
 - If possible, how many NULL values can be inserted
 - Whether collection class provide sorting, by default
 - Is there any way to apply customized sorting
 - Performance, while dealing with retrieval or manipulation (addition/deletion)
 - By default, all methods are synchronized or Not
 
Related Articles:
- How to get all keys of a HashMap
 - How to get all values of a HashMap
 - How to get all Entries or Key-Value pairs of HashMap
 - How to get size or length of HashMap
 - How to check whether a particular key is present in HashMap
 - How to check whether a particular value is present in HashMap
 - How to check whether HashMap is empty or not ?
 - Adding one HashMap to another HashMap using putAll method
 - How to delete an entry of HashMap
 - How to delete all entries of HashMap
 - How to remove an entry from HashMap by comparing values in Java 8
 - How to remove an entry from HashMap by comparing keys in Java 8
 - Various ways to iterate through HashMap
 - To reverse order the LinkedHashMap contents
 - Map: How ConcurrentModificationException can be handled in Java
 
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
 - https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
 - https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
 - https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
 - https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
 - https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
 
Happy Coding !!
Happy Learning !!
