Map in Java
Source: Dev.to

What is Map in Java Collections?
A Map in Java (from the Java Collections Framework) is a data structure that stores data in key–value pairs.
- Each key is unique.
- Each value is associated with a key.
Think of it like a dictionary: word → meaning.
Example
101 → "John"
102 → "Alice"
Why do we use Map?
We use a Map when:
- We need fast lookup using a key.
- We want to store pairs of related data.
- We need unique keys.
Real‑world examples
- Student ID → Name
- Username → Password
- Product ID → Price
Common Map Implementations
- HashMap – Fast, unordered.
- LinkedHashMap – Maintains insertion order.
- TreeMap – Sorted by keys.
Important Methods
put(key, value)– Insert data.get(key)– Retrieve the value for a key.remove(key)– Delete the entry for a key.containsKey(key)– Check if a key exists.keySet()– Get a set of all keys.
