In Java, maps are data structures that associate keys with values, allowing key-value pairs to be stored, accessed, updated, and removed efficiently. Unlike collections that store only elements, maps maintain a mapping relationship in which each key is unique and points to exactly one value, although values may be repeated.

The Map interface provides mechanisms for obtaining values from unique (non-duplicated) keys. The Map interface provides three collection views that allow the contents of a map to be manipulated in different ways: a set of keys, a collection of values, or a set of mappings between keys and values. The main interface derived from Map is SortedMap, which represents sorted maps, and its subinterface NavigableMap. Map is widely used by classes in the standard library. The main classes that implement the Map interface for non-concurrent maps are HashMap, LinkedHashMap, and TreeMap. For concurrent maps, the main classes are ConcurrentHashMap and ConcurrentSkipListMap.

The following is part of the definition of the Map interface, containing only the main methods:

public interface Map<K,V>{
    // Query Operations
    int size();
    boolean isEmpty();
    boolean containsKey(Object key);
    boolean containsValue(Object value);
    V get(Object key);

    // Modification Operations
    V put(K key, V value);
    V remove(Object key);
    void putAll(Map<? extends K, ? extends V> m);
    void clear();

    // Views
    Set<K> keySet();
    Collection<V> values();
    Set<Map.Entry<K, V>> entrySet();

    // Definition of the Entry interface
    interface Entry<K, V> {
        // Body omitted
    }

    // (continues...)
}

The following example shows a map of cars whose keys are license plates and whose values are the names of the corresponding car models. With this map, we can, for example, test whether a given element exists by using the key as the parameter to the containsKey method. In addition, the get method obtains the value associated with the key passed as a parameter, and the remove method removes the entry from the map:

void main() {
    Map<String, String> cars = new HashMap<>();
    cars.put("ABCD2935", "Tesla Model S");
    cars.put("YJKF9353", "Toyota Corolla");
    cars.put("WFLO1304", "Honda CRV");

    var key = "ABCD2935";

    if (cars.containsKey(key)){
        IO.println("Vehicle data:");
        IO.println(cars.get(key));
        IO.println("Removing the vehicle...");
        cars.remove(key);
    } else
        IO.println("Vehicle not found!");
}

The output would be:

Vehicle data:
Tesla Model S
Removing the vehicle...

In addition to the methods described above, there are also several default methods that implement additional functionality.

The interface also provides methods for creating immutable maps from supplied values (the of and ofEntries methods, along with the auxiliary entry method) or from an existing map (copyOf).

Figure 1 shows the Map interface and its main subinterfaces. The direct subinterfaces of Map are SequencedMap, Bindings, and ConcurrentMap. Indirectly, SortedMap, NavigableMap, and ConcurrentNavigableMap are also interfaces derived from Map (the latter is also a subinterface of ConcurrentMap).

Map interface and its subinterfaces

Among the most important descendants of Map are the SortedMap and NavigableMap interfaces.

The SortedMap interface is an extension of the SequencedMap interface that represents maps that maintain their keys in a specific order, according to the natural ordering of the keys or according to a specific comparator supplied when the map is created. Its only direct subinterface in the collections framework, NavigableMap, adds methods that provide more advanced navigation operations for manipulating map keys, such as obtaining the key closest to a specific value or iterating over the map in ascending or descending order.

In practice, we commonly use NavigableMap rather than SortedMap, since both classes that implement SortedMap in the collections framework do so indirectly (TreeMap implements NavigableMap, and ConcurrentSkipListMap implements ConcurrentNavigableMap). NavigableMap goes a little further in terms of functionality, providing specific methods for navigating through the map, in a way analogous to what NavigableSet provides for sets.


This article is adapted from content in the book Orientação a Objetos com Java, by Eduardo Kessler Piveta