English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java list (List)

Java Queue (queue)

Java Map collection

Java Set collection

Java input output (I/O)

Java Reader/Writer

Java other topics

Java Map Interface

In this tutorial, we will learn about the Java Map interface and its methods.

The Map interface of the Java collections framework provides the functionality of the Map data structure.

It implements the Collection interface.

The workflow of map

In Java, Map elements are stored in keys/In the value pair. The key is the unique value associated with each value.

Map collections cannot contain duplicate keys. And, each key is associated with a value.

We can use the keys associated with them to access and modify the values.

In the figure above, we have values: United States, Brazil, and Spain. We all have the corresponding keys: us, br, and es.

Now, we can use their corresponding keys to access these values.

Note:Map interface maintains3different collections:

  • Key set

  • Value set

  • Key/A collection of value associations (Map collections).

Therefore, we can separately access keys, values, and associations.

Classes implementing Map

Since Map is an interface, it is not possible to create an object from it.

To use the functions of the Map interface, we can use the following classes:

These classes are defined and implemented in the collection framework and implement the Map interface.

Interfaces inheriting Map

The Map interface is also inherited by the following subinterfaces:

How to use map?

In Java, we must import the java.util.Map package to use the Map. After importing the package, we will create the map as follows.

//Create a Map using the HashMap class
Map<Key, Value> numbers = new HashMap<>();

In the above code, we created a Map named numbers. We have used the HashMap class to implement the Map interface.

Here,

  • Key - A unique identifier used to associate each element (value) in the map

  • Value - Elements associated with the key in the map

map methods

The Map interface includes all methods of the Collection interface. This is because Collection is the superinterface of Map.

In addition to the methods available in the Collection interface, the Map interface also includes the following methods:

  • put(K, V) - Insert the association of key K and value V into the map. If the key already exists, the new value will replace the old value.

  • putAll() - Insert all entries from the specified Map collection into this Map collection.

  • putIfAbsent(K, V) - Insert the association V if the key K is not yet associated with a value.

  • get(K) - Return the value associated with the specified key K. If the key is not found, return null.

  • getOrDefault(K, defaultValue) - Return the value associated with the specified key K. If the key is not found, return defaultValue.

  • containsKey(K) - Check if the specified key K exists in the map.

  • containsValue(V) - Check if the specified value V exists in the map.

  • replace(K, V) - Replace the value of key K with a new specified value V.

  • replace(K, oldValue, newValue) - Replace the value associated with key K with a new value newValue only if the key K is associated with the oldValue.

  • remove(K) - Remove the entry from the Map represented by key K.

  • remove(K, V) - Remove the entry associated with key K and value V from the Map collection...

  • keySet() -Returns a set of all existing keys in the Map collection.

  • values() -Returns a set of all values contained in the Map collection.

  • entrySet() -Returns all existing keys in the map/Collection of value mappings.

Implementation of the map interface

1.Implementation of HashMap class

import java.util.Map;
import java.util.HashMap;
class Main {
    public static void main(String[] args) {
        //Creating map using HashMap class
        Map<String, Integer> numbers = new HashMap<>();
        //Insert elements into map collection
        numbers.put("One", 1);
        numbers.put("Two", 2);
        System.out.println("Map: ") + numbers);
        //map keys
        System.out.println("Keys: ") + numbers.keySet());
        //map values
        System.out.println("Values: ") + numbers.values());
        //map entries
        System.out.println("Entries: ") + numbers.entrySet());
        //Remove element from map collection
        int value = numbers.remove("Two");
        System.out.println("The value deleted is: ") + value);
    }
}

Output result

Map: {One =1, Two =2}
Keys: [One, Two]
Values: [1, 2]
Entries: [One =1, Two =2]
The value deleted is: 2

For more information about HashMap, please visitJava HashMap.

2.Implementation of TreeMap class

import java.util.Map;
import java.util.TreeMap;
class Main {
    public static void main(String[] args) {
        //Creating map using TreeMap
        Map<String, Integer> values = new TreeMap<>();
        //Inserting elements into map
        values.put("Second", 2);
        values.put("First", 1);
        System.out.println("Using TreeMap to create map: " + values);
        //Replacement value
        values.replace("First", 11);
        values.replace("Second", 22);
        System.out.println("New Map: ", + values);
        //Remove element from map collection
        int removedValue = values.remove("First");
        System.out.println("Removed value: ", + removedValue);
    }
}

Output result

Create map using TreeMap: {First=1, Second=2}
New Map: {First=11, Second=22}
Removed value: 11

For more information on TreeMap, please visitJava TreeMap.