Java Map Interface
The Map interface is a part of the Java Collections Framework and is used
to store key-value pairs. Each key must be unique, but values can be
duplicated.
A Map is useful when you want to associate a key (like a name or ID) with
a value (like an age or description).
Common classes that implement Map:
- HashMap - fast and unordered
- TreeMap - sorted by key
- LinkedHashMap - ordered by insertion
Tip: Use a Map when you want to associate values with unique keys,
like storing user IDs with names.
Common Map Methods
Table
Method
put()
Description
Adds or updates a key-value pair
get()
Returns the value for a given key
remove()
containsKey()
keySet()
Removes the key and its value
Checks if the map contains the key
Returns a set of all keys