Java Map Interface

In Java Map Interface is one of the most important topics. The map interface is considered as apart of the Collection framework, but it doesn’t implement the Collection interface. In this post, we will read What is Java Map Interface and how it is implemented by other classes.
The Map interface presents in java.util package and it represents the mapping of key and value. A map maintains the data in two parts one is valued, and another is key. Map stores each value with a unique key that is known as an entry. A Map doesn’t allow duplicate keys, but it can have duplicate values.
We can’t traverse a Map directly; we have to convert it into
Set using keySet() or entrySet() method. The Map interface has its implementation in various classes like HashMapLinkedHashMap, and TreeMap.

Java Map Interface

Characteristics of Map Interface

1. The Map interface doesn’t implement the Collection interface. So it’s characteristics are different from the rest of the collection types.

2. The Map interface provides different views of the collection. It provides some view that contains a set of keys, a set of key-value mappings, and a collection of values.

3. A map contains a pair of keys and values. It doesn’t contain duplicate keys, but it can contain duplicate values. Each value associated with the unique key.

4. A Map interface doesn’t maintain the order of elements; it depends on the implementation. For example, HashMap doesn’t guarantee the order of mappings but TreeMap does.

Java map methods

  1. put(Object key, Object value): This method is used to insert an entry in this map. It takes two parameters one is key, and another is value.
  2. putAll(Map map): This method is used to copy all elements of the specified map in the current map.
  3. remove(Object key): This method is used to remove an entry for the specified key.
  4. get(Object key): This method is used to get the value for the specified key.
  5. containsKey(Object key): This method is used to check whether the specified key exists or not. Its return type is boolean.
  6. keySet(): This method is used to get the Set view containing all the keys. It returns all the key set.
  7. entrySet(): This method is used to get the Set view containing all the keys and values. It returns all the key and values set.
  8. size(): It is used to check the number of key-value pairs present in Map.??
  9. isEmpty(): This method is used to check whether the map contains any key-value pair or not. Its return type is boolean.
  10. containsValue(Object value): This method is used to check whether the specified value exists in the map or not. Its return type is boolean.
  11. clear(): This method is used to remove all elements from the Map. It doesn’t return anything, so its return type is void.
  12. values(): This method is used to get all values of the Map. It returns Collection type.

Implementations of Map

The Map interface is implemented by several classes but there are three major implementations that are most popular. Here we will discuss the HashMap, LinkedHashMap, and TreeMap.

HashMap Class

The most important and common implementation of the Map interface is the HashMap class. A HashMap implements all the methods of Map interface and provides the body to the methods. A HashMap doesn’t maintain any order because all values associated keys. It allows only null values and one null key.  Let’s see the java HashMap example.

import java.util.HashMap;
import java.util.Map;

class HashMapExample
{
	public static void main(String arg[])
	{
		Map<Integer, String> hashMap = new HashMap<Integer, String>();
		hashMap.put(1, "Hi");
		hashMap.put(2, "Java");
		hashMap.put(3, "Goal");
		hashMap.put(4, "Learning");
		hashMap.put(5, "Website");
		
		System.out.println("Get all keys: "+ hashMap.keySet());
		System.out.println("Get all values: "+ hashMap.values());
		System.out.println("Get all entry set: "+ hashMap.entrySet());
	}
}

Output: Get all keys: [1, 2, 3, 4, 5]
Get all values: [Hi, Java, Goal, Learning, Website]
Get all entry set: [1=Hi, 2=Java, 3=Goal, 4=Learning, 5=Website]

LinkedHashMap class

Like HashMap, LinkedHashMapclass is also an implementation of Map. A LinkedHashMap maintains an order of elements inserted into it. Let’s see the LinkedHashMap example.

import java.util.LinkedHashMap;
import java.util.Map;

class HashMapExample
{
	public static void main(String arg[])
	{
		Map<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>();
		linkedHashMap.put(1, "Hi");
		linkedHashMap.put(2, "Java");
		linkedHashMap.put(3, "Goal");
		linkedHashMap.put(4, "Learning");
		linkedHashMap.put(5, "Website");
		
		System.out.println("Get all keys: "+ linkedHashMap.keySet());
		System.out.println("Get all values: "+ linkedHashMap.values());
		System.out.println("Get all entry set: "+ linkedHashMap.entrySet());
	}
}

Output: Get all keys: [1, 2, 3, 4, 5]
Get all values: [Hi, Java, Goal, Learning, Website]
Get all entry set: [1=Hi, 2=Java, 3=Goal, 4=Learning, 5=Website]

TreeMap class

A TreeMap implements the Map interface like HashMap. It maintains the order of elements. It doesn’t allow null key but we can add null as a value. Let’s see the TreeMap example.

import java.util.Map;
import java.util.TreeMap;

class HashMapExample
{
	public static void main(String arg[])
	{
		Map<Integer, String> linkedHashMap = new TreeMap<Integer, String>();
		linkedHashMap.put(1, "Hi");
		linkedHashMap.put(2, "Java");
		linkedHashMap.put(3, "Goal");
		linkedHashMap.put(4, "Learning");
		linkedHashMap.put(5, "Website");
		linkedHashMap.put(6, null);
		
		System.out.println("Get all keys: "+ linkedHashMap.keySet());
		System.out.println("Get all values: "+ linkedHashMap.values());
		System.out.println("Get all entry set: "+ linkedHashMap.entrySet());
	}
	
}

Output: Get all keys: [1, 2, 3, 4, 5, 6]
Get all values: [Hi, Java, Goal, Learning, Website, null]
Get all entry set: [1=Hi, 2=Java, 3=Goal, 4=Learning, 5=Website, 6=null]

Leave a Comment