Map Interface: Key-Value Associations
You'll learn: What the Map interface is and how it differs fundamentally from the Collection interface you've been using.
Not a Collection
Here's a key surprise: Map is not part of the Collection hierarchy. Unlike List, Set, Queue, and Deque—which all extend the Collection interface—Map stands alone. Why? Because it stores pairs of data (key-value associations), not single elements.
The Key-Value Model
Think of a Map like a real-world dictionary:
- Keys are like the words you look up (must be unique)
- Values are like the definitions (can be duplicated)
- You use the word (key) to instantly find its meaning (value)
In a Map<String, Integer>, each String key maps to exactly one Integer value. No two entries can have the same key, but multiple keys can point to the same value.
Core Operations
The Map interface provides fundamentally different methods than Collection:
put(K key, V value)– Associates a value with a key (like adding a dictionary entry)get(Object key)– Retrieves the value for a given key (like looking up a word)containsKey(Object key)– Checks if a key exists in the map
Notice there's no add() method like in Collection—you don't "add an element," you "put a key-value pair."
Why This Matters
Because Map isn't a Collection, you can't use it with methods that expect Collection<E>. The two interfaces serve different purposes: Collection manages groups of things, while Map manages associations between things.
Key Takeaway:
Map is a separate interface from Collection that stores key-value pairs, using put(), get(), and containsKey() instead of typical collection operations.