MAP Interface in Java : Complete tutorial and Interview question

Map in java used to represent the represent the group of objects as key-value pair whereas collection concept is to represent the group of objects as a single entity.

Example : Suppose we want to store the customer Id and Customer name together then we can use MAP. Below we have described this a java program.

Features of MAP

1 ) It is not the child interface of collection framework as it do not implements the Map interface but it comes under collections in JAVA.
2 ) Key and values are objects in MAP.
3 ) Duplicate keys are not allowed.
4 ) Duplicate values are allowed.
5) Null values are not allowed in Map.
6 ) It introduced in 1.2 version.


Some Common methods of Map

1) void clear( ) : Removes all key/value pairs from the invoking map.

2) boolean containsKey(Object k) : Returns true if the invoking map contains k as a key. Otherwise, returns false.

3) boolean containsValue(Object v) : Returns true if the map contains v as a value. Otherwise, returns false.

4) Set<map.entry<k, v=””>> entrySet( ) : Returns a Set that contains the entries in the map. The set contains objects of type Map.Entry. Thus, this method provides a set-view of the invoking map.

5) boolean equals(Object obj) : Returns true if obj is a Map and contains the same entries. Otherwise, returns false.

6) V get(Object k) :  Returns the value associated with the key k. Returns null if the key is not found.

7 ) int hashCode( ) : Returns the hash code for the invoking map.

8) boolean isEmpty( ) : Returns true if the invoking map is empty. Otherwise, returns false.

9) Set keySet( ) : Returns a Set that contains the keys in the invoking map. This method provides a set-view of the keys in the invoking map.

10) V put(K k, V v) : Puts an entry in the invoking map, overwriting any previous value associated with the key. The key and value are k and v, respectively. Returns null if the key did not already exist. Otherwise, the previous value linked to the key is returned.

11 ) void putAll(Map m) :  Puts all the entries from m into this map.

12 ) V remove(Object k) : Removes the entry whose key equals k.

13 ) int size( ) : Returns the number of key/value pairs in the map.

14 ) Collection values( ) : Returns a collection containing the values in the map. This method provides a collection-view of the values in the map.


example : we are storing the key-value pair in Map object where key is customer Id and value is customer name. Below are the Map object  and collection objects both. In Map object entries are stored in key-value pair where as in Collection only values are there.

Map-in-java

Collection-object-in-java


Java program to define some of the common methods of Map

import java.util.*;

public class map_interface {

	public static void main(String[] args) {
		
		// creating hashmap object 
		 Map map=new HashMap<Integer,String>();  
		  // put method to insert values ( keys, values)
		  map.put(001,"Suresh");  
		  map.put(002,"Ramesh");  
		  map.put(003,"Deepak");
		  map.put(004,"Ravi");
		  System.out.println(map);
		 //output -> {1=Suresh, 2=Ramesh, 3=Deepak, 4=Ravi} 

		  // method contains Keys
		  System.out.println(map.containsKey(003));
		 // output -> true 
		  // 003 key is contained in map 
		  
		  // method  containsValue(Object v) 
		  System.out.println(map.containsValue("Ravi"));
		  // output -> true
		  // value Ravi is contained in Map
		  
		  // method get(Object k) 
		  System.out.println(map.get(002));
		  // output -> Ramesh
		  // Value ramesh is returned for key 002
		  
		  // method int size 
		  System.out.println(map.size());
		  // output - > 4
		  // size of the map objects - total 4 enteries are present
		  
		  
	}

}

Implementation Classes Of MAP

1 ) HashMap Class : It introduced in 1.2 version.
a ) LinkedHashMap Class : It introduced in 1.2 version.

2 ) WeakHash Map Class : It introduced in 1.2 version.

3 ) IdentityHashMap Class : It introduced in 1.4 version.

4 ) Hashtable Class : It introduced in 1.0 version. — > Hastable implements Dictionay ( which is a abstract Class )
a ) Properties Class : It introduced in 1.0 version.

we will discuss all these classes in detail in later tutorials.


SortedMap Interface in JAVA

when we need to represent the object in key value pair according to some sorting order of Keys.

Features of Sorted Map

1 ) It is the child interface of MAP.
2 ) Sorting is based on Keys not on value.
3 ) It introduced in 1.2 version.


Navigable Map Interface

Features of Navigable Map

1 ) Is is the child interface of sorted Map.
2 ) It defines several methods for Navigation purposes.
3 ) It introduced in 1.6 version.
4) TreeMap class implements Navigable interface.


This is the brief introduction of Map interface of Java , SortedMap interface and navigableMap are described in detail in later tutorials.Here we have covered all about the Map interface.