Introduction to the Map Interface in Java

Overview

The Map interface is a part of the Collection framework. However, Map doesn’t implement Collection. Why is it a part of the Collection framework? It’s because Map by nature is a collection of elements.

Implementations of the Map interface are used in other members of the Collection framework. One example is the HashSet class uses HashMap internally to achieve the uniqueness of its elements.

Here is the overview of the Map interface:

The Map interface family in Java

Basic Map operations

The Map interface provides methods to interact with elements. Here are the list of common methods that Map offers

Method nameFunction
clear()remove all entries
remove(K key)remove an entry with a specific key
put(E e)Put an entry to a map
putAll(Map m)Put all entries of the map m into this map
size()Get the number of entries in the map
values()Get all values of the entries and put them into a collection
get(K key)get the value of a specific key
keySet()Get all keys in a Set
containsKey(K key)Check if the map contains a key
containsValue(V value)check if the map has key(s) map to the value
putIfAbsent(K key, V value)If the key doesn’t exist or is currently mapped to null, put the entry

There are more methods available and you can get all here in the documentation.

Map operations in action

Let’s write some code to see the methods in action

package com.datmt.java_core.collection.map;

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

public class MapOperation {
    public static void main(String[] args) {
        basicMapOperations();
    }

    private static void basicMapOperations() {
        Map<String, Integer> peopleAge = new HashMap<>();

        //add entries to map
        peopleAge.put("Jane", 20);

        //add multiple entries
        peopleAge.putAll(Map.of("Lance", 30, "Jake", 33));

        //put a mapping to null
        peopleAge.put("Leo", null);

        System.out.println("People age with null value: "  + peopleAge);

        //put if absent

        //add a null
        peopleAge.putIfAbsent("Jane", 55);//this will not change Jane's age
        peopleAge.putIfAbsent("Leo", 11);//This will update Leo's age

        System.out.println("People age after updating absent: "  + peopleAge);

        //Check contains
        System.out.println("the map contains entry for Jake? " + peopleAge.containsKey("Jake"));

        System.out.println("There is person with age 20 in the map? " + peopleAge.containsValue(20));

        //Get all values as a set
        System.out.println("All ages of people: " + peopleAge.values());

        //Get all keys of the map
        System.out.println("All people in this map: " + peopleAge.keySet());

        //Remove entry
        peopleAge.remove("Leo");

        System.out.println("The map now without Leo: " + peopleAge);

        //Remove all entries
        peopleAge.clear();

        System.out.println("The map now is empty: " + peopleAge);

    }
}

The output of the above code is:

map operation in action

Conclusion

This is a quick introduction to the Map interface. There are sub-classes and sub-interfaces that offer more functionalities for more specific use cases. We’ll cover them next.

Leave a Comment