Collections Framework - The Map Interface

Map interface Overview

  • 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 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 Simple Example

Let's create a simple example to demonstrate Map interface with one of it's implementation class - HashMap.
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 APIs/Methods

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

Map Interface Implementations

The Java platform contains three general-purpose Map implementations:
We will learn Map interface with these implementation classes with examples. There separate post for each Map Interface implementations and you will learn below topics in each implementation class.

HashMap Implementation Class

  1. Map interface Overview
  2. HashMap class Overview
  3. Creating a HashMap and Adding key-value pairs to it
  • put(String key, Integer value)
  • putIfAbsent(String key, Integer value)
  1. HashMap API's for accessing keys and modifying their associated value
  • isEmpty()
  • size()
  • containsKey(Object key)
  • containsValue(Object value)
  • get(Object key)
  1. HashMap remove API's with Examples
  • remove(Object key)
  • remove(Object key, Object value)
  1. HashMap demonstration for null keys and null values
  2. How to perform range view operations in Map?
  • keySet()
  • values()
  • entrySet()
  1. Different ways to iterate over Map
  2. How to store multiple values in Map?
  3. Java 8 forEach() method with Map
  4. Synchronizing Access to Java HashMap

LinkedHashMap Implementation Class

  1. Overview of LinkedHashMap class
  2. Creating and Initializing a LinkedHashMap
  • put(String key, Integer value)
  • putIfAbsent(String key, Integer value)
  1. LinkedHashMap API's for accessing keys and modifying their associated value
  • isEmpty()
  • size()
  • containsKey(Object key)
  • containsValue(Object value)
  • get(Object key)
  1. HashMap remove API's with Examples
  • remove(Object key)
  • remove(Object key, Object value)
  1. Iterating over a LinkedHashMap
  • Iterate over a LinkedHashMap using Java 8 forEach and lambda expression.
  • Iterate over a LinkedHashMap’s entrySet using Java 8 forEach and lambda expression.
  • Iterate over a LinkedHashMap’s entrySet using iterator().
  • Iterate over a LinkedHashMap’s entrySet using iterator() and Java 8 forEachRemaining() method.
  1. LinkedHashMap Maintain Insertion Order Example
Learn LinkedHashMap on Guide to LinkedHashMap Class

TreeMap Implementation Class

  1. Overview of TreeMap class
  2. Create TreeMap Example
  3. TreeMap Sorting Order Example(Ascending Order)
  4. TreeMap with a custom Comparator (Descending Order)
  5. Accessing the entries of a TreeMap

Learn TreeMap on Guide to TreeMap Class

Comments