The perfect place for easy learning...

Java Programming

×

Topics List


Map Interface Classes in java





The java collection framework has an interface Map that is available inside the java.util package. The Map interface is not a subtype of Collection interface.

The Map interface has the following three classes.

Class Description
HashMap It implements the Map interface, but it doesn't maintain any order.
LinkedHashMap It implements the Map interface, it also extends HashMap class. It maintains the insertion order.
TreeMap It implements the Map and SortedMap interfaces. It maintains the ascending order.

Commonly used methods defined by Map interface

Method Description
Object put(Object k, Object v) It performs an entry into the Map.
Object putAll(Map m) It inserts all the entries of m into invoking Map.
Object get(Object k) It returns the value associated with given key.
boolean containsKey(Object k) It returns true if map contain k as key. Otherwise false.
Set keySet() It returns a set that contains all the keys from the invoking Map.
Set valueSet() It returns a set that contains all the values from the invoking Map.
Set entrySet() It returns a set that contains all the entries from the invoking Map.

Now, let's look at each class in detail with example programs.

HashMap Class

The HashMap class is a child class of AbstractMap, and it implements the Map interface. The HashMap is used to store the data in the form of key, value pair using hash table concept.

Key Properties of HashMap

  • HashMap is a child class of AbstractMap class.
  • HashMap implements the interfeaces Map, Cloneable, and Serializable.
  • HashMap stores data as a pair of key and value.
  • HashMap uses Hash table concept to store the data.
  • HashMap does not allow duplicate keys, but values may be repeated.
  • HashMap allows only one null key and multiple null values.
  • HashMap does not follow any oreder.
  • HashMap has the default capacity 16 entries.

Let's consider an example program to illustrate HashMap.

Example
import java.util.*;

public class HashMapExample {

	public static void main(String[] args) {
		
		Scanner read = new Scanner(System.in);

		HashMap employeeInfo = new HashMap();
		HashMap contactInfo = new HashMap();

		employeeInfo.put(1, "Raja");
		employeeInfo.put(2, "Gouthami");
		employeeInfo.put(3, "Heyansh");
		employeeInfo.put(4, "Yamini");
		employeeInfo.put(5, "ManuTej");
			
		System.out.println("Employee Information\n" + employeeInfo);

		System.out.println("\nPlease enter the ID and Contact number");
		System.out.println("Employee IDs : " + employeeInfo.keySet());
		System.out.print("Enter ID: ");
		int id = read.nextInt();
		System.out.print("Enter Contact Number: ");
		long contactNo = read.nextLong();
		if(employeeInfo.containsKey(id)) {
			contactInfo.put(id, contactNo);
		}
		
		System.out.println("\n\nEmployee Contact Information\n");
		System.out.println("ID - " + id);
		System.out.println("Name - " + employeeInfo.get(id));
		System.out.println("Contact Number - " + contactInfo.get(id));
		
	}

}

HashMap class in java

LinkedHashMap Class

The LinkedHashMap class is a child class of HashMap, and it implements the Map interface. The LinkedHashMap is used to store the data in the form of key, value pair using hash table and linked list concepts.

Key Properties of LinkedHashMap

  • LinkedHashMap is a child class of HashMap class.
  • LinkedHashMap implements the Map interface.
  • LinkedHashMap stores data as a pair of key and value.
  • LinkedHashMap uses Hash table concept to store the data.
  • LinkedHashMap does not allow duplicate keys, but values may be repeated.
  • LinkedHashMap allows only one null key and multiple null values.
  • LinkedHashMap follows the insertion oreder.
  • LinkedHashMap has the default capacity 16 entries.

Let's consider an example program to illustrate LinkedHashMap.

Example
import java.util.*;

public class HashMapExample {

	public static void main(String[] args) {
		
		Scanner read = new Scanner(System.in);

		LinkedHashMap employeeInfo = new LinkedHashMap();
		LinkedHashMap contactInfo = new LinkedHashMap();

		employeeInfo.put(1, "Raja");
		employeeInfo.put(2, "Gouthami");
		employeeInfo.put(3, "Heyansh");
		employeeInfo.put(4, "Yamini");
		employeeInfo.put(5, "ManuTej");
			
		System.out.println("Employee Information\n" + employeeInfo);

		System.out.println("\nPlease enter the ID and Contact number");
		System.out.println("Employee IDs : " + employeeInfo.keySet());
		System.out.print("Enter ID: ");
		int id = read.nextInt();
		System.out.print("Enter Contact Number: ");
		long contactNo = read.nextLong();
		if(employeeInfo.containsKey(id)) {
			contactInfo.put(id, contactNo);
		}
		
		System.out.println("\n\nEmployee Contact Information\n");
		System.out.println("ID - " + id);
		System.out.println("Name - " + employeeInfo.get(id));
		System.out.println("Contact Number - " + contactInfo.get(id));
		
	}

}

LinkedHashMap class in java

TreeMap Class

The TreeMap class is a child class of AbstractMap, and it implements the NavigableMap interface which is a child interface of SortedMap. The TreeMap is used to store the data in the form of key, value pair using a red-black tree concepts.

Key Properties of TreeMap

  • TreeMap is a child class of AbstractMap class.
  • TreeMap implements the NavigableMap interface which is a child interface of SortedMap interface.
  • TreeMap stores data as a pair of key and value.
  • TreeMap uses red-black tree concept to store the data.
  • TreeMap does not allow duplicate keys, but values may be repeated.
  • TreeMap does not allow null key, but allows null values.
  • TreeMap follows the ascending oreder based on keys.

Let's consider an example program to illustrate TreeMap.

Example
import java.util.*;

public class HashMapExample {

	public static void main(String[] args) {
		
		Scanner read = new Scanner(System.in);

		TreeMap employeeInfo = new TreeMap();
		TreeMap contactInfo = new TreeMap();

		employeeInfo.put(1, "Raja");
		employeeInfo.put(4, "Gouthami");
		employeeInfo.put(5, "Heyansh");
		employeeInfo.put(3, "Yamini");
		employeeInfo.put(2, "ManuTej");
			
		System.out.println("Employee Information\n" + employeeInfo);

		System.out.println("\nPlease enter the ID and Contact number");
		System.out.println("Employee IDs : " + employeeInfo.keySet());
		System.out.print("Enter ID: ");
		int id = read.nextInt();
		System.out.print("Enter Contact Number: ");
		long contactNo = read.nextLong();
		if(employeeInfo.containsKey(id)) {
			contactInfo.put(id, contactNo);
		}
		
		System.out.println("\n\nEmployee Contact Information\n");
		System.out.println("ID - " + id);
		System.out.println("Name - " + employeeInfo.get(id));
		System.out.println("Contact Number - " + contactInfo.get(id));
		
	}

}

TreeMap class in java