Java Collections Framework - The Map Interface

In this guide, we will learn about Map interface, Map interface methods and Map interface implementation classes.

Important Key Points About Map Interface

  • Map interface is a part of Java Collection Framework, but it doesn’t inherit Collection Interface.
  • A Map cannot contain duplicate keys:  Each key can map to at most one value. It models the mathematical function abstraction.
  • Each key at most must be associated with one value.
  • Each key-value pairs of the map are stored as Map.Entry objects. Map.Entry is an inner interface of Map interface.
  • The Java platform contains three general-purpose Map interface implementations: HashMap, TreeMap, and LinkedHashMap.
  • Order of elements in a map is implementation dependent. HashMap doesn’t maintain any order of elements. LinkedHashMap maintains insertion order of elements. Where as TreeMap places the elements according to the supplied Comparator.
  • The Map interface provides three methods, which allows map’s contents to be viewed as a set of keys (keySet() method), a collection of values (values() method), or set of key-value mappings (entrySet() method).

Map Interface with It's HashMap Implementation Class Example

Let's create a simple example to demonstrate Map interface with it's HashMap implementation class:
import java.util.HashMap;
import java.util.Map;

public class CreateHashMapExample {
    public static void main(String[] args) {
        // Creating a HashMap
     Map<String, Integer> numberMapping = new HashMap<>();

        // Adding key-value pairs to a HashMap
        numberMapping.put("One", 1);
        numberMapping.put("Two", 2);
        numberMapping.put("Three", 3);

        // Add a new key-value pair only if the key does not exist in the HashMap, or is mapped to `null`
        numberMapping.putIfAbsent("Four", 4);

        System.out.println(numberMapping);
    }
}
Output:
{One=1, Four=4, Two=2, Three=3}

Map Interface with It's LinkedHashMap Implementation Class Example

Let's create a simple example to demonstrate Map interface with it's LinkedHashMap implementation class:
// Creating a LinkedHashMap
LinkedHashMap<String, Integer> dayNumberMapping = new LinkedHashMap<>();

// Adding new key-value pairs to the LinkedHashMap
dayNumberMapping.put("Mon", 1);
dayNumberMapping.put("Tus", 2);
dayNumberMapping.put("Wen", 3);
dayNumberMapping.put("Thu", 4);
dayNumberMapping.put("Fri", 5);
dayNumberMapping.put("Sat", 6);

// Add a new key-value pair only if the key does not exist 
// in the LinkedHashMap, or is mapped to `null`
dayNumberMapping.putIfAbsent("Sun", 7);

System.out.println(dayNumberMapping);
Output:
{Mon=1, Tus=2, Wen=3, Thu=4, Fri=5, Sat=6, Sun=7}

Map Interface with It's TreeMap Implementation Class Example

Let's create a simple example to demonstrate Map interface with it's TreeMap implementation class:
// Creating a TreeMap
TreeMap<String, String> fileExtensions  = new TreeMap<>();

// Adding new key-value pairs to a TreeMap
fileExtensions.put("python", ".py");
fileExtensions.put("c++", ".cpp");
fileExtensions.put("kotlin", ".kt");
fileExtensions.put("golang", ".go");
fileExtensions.put("java", ".java");

// Printing the TreeMap (Output will be sorted based on keys)
System.out.println(fileExtensions);
Output:
{c++=.cpp, golang=.go, java=.java, kotlin=.kt, python=.py}

Map Interface APIs/Methods

The class diagram shows a list of APIs/Methods Map interface provides.
Read more about each API with an example in Guide to HashMap Class

Map Interface Implementations

General-purpose Map Implementations:

Related Collections Framework Interfaces

Comments