The Map ADT

Here is the Map ADT:

public interface Map<K, V> {
  boolean put(K key, V value);
  V get(K key);
  boolean remove(K key);
  boolean containsKey(K key);
  int size();
}

Look at the header first: Map<K, V>. Our generic classes so far had one type parameter, as in Set<T>. A map holds a key and a value per entry, and the two need not have the same type: a username maps to a password, both strings, but a product id (an Integer) might map to a name (a String). So the interface declares two type parameters, K for the key type and V for the value type.

This interface has five methods, one more than the set, because we need get to fetch the value stored under a key. The put method stores a value under a key, replacing the old value if the key is already present. The other three work as they do for a set, with keys in place of items.

The interface has no way to get an entry by index and no way to go from a value back to its key. Like a set, a map is not positional. It holds key–value pairs with distinct keys in no particular order, and you reach a value only through its key.

  ┌──────────────────────────────────┐
  │   "ann" → "x7"   "bo" → "q3"     │
  │         "cal" → "m9"             │
  └──────────────────────────────────┘
  each key appears once; no order