Map in Java
Introduction
A Map in Java is a collection that stores data in the form of key-value pairs. Each key is unique and is used to access its corresponding value.
The Map interface is part of the Java Collection Framework and is available in the java.util package.
import java.util.Map;
Example
Key Value ----------------- 101 Anu 102 Ram 103 Sita
Here, 101, 102, and 103 are keys, while Anu, Ram, and Sita are values.
Features of Map
- Stores data as key-value pairs.
- Each key must be unique.
- Values can be duplicated.
- Provides fast searching using keys.
- Supports adding, updating, and removing data.
- Different Map implementations provide different ordering behavior.
Map Syntax
Map<KeyType, ValueType> mapName = new HashMap<>();
Example:
Map<Integer, String> students = new HashMap<>();
Example of Map
import java.util.*; public class MapExample { public static void main(String[] args) { Map<Integer, String> students = new HashMap<>(); students.put(101, "Anu"); students.put(102, "Ram"); students.put(103, "Sita"); System.out.println(students); } }
Output
{101=Anu, 102=Ram, 103=Sita}
Note:
HashMapdoes not guarantee any particular order.
Adding and Updating Elements
The put() method is used to add a key-value pair.
Map<Integer, String> students = new HashMap<>(); students.put(101, "Anu"); students.put(102, "Ram");
If the same key is used again, its value is updated.
students.put(101, "Sita");
Now key 101 contains "Sita" instead of "Anu".
Common Map Methods
| Method | Description |
|---|---|
put() | Adds or updates a key-value pair |
get() | Returns the value associated with a key |
remove() | Removes a key-value pair |
containsKey() | Checks whether a key exists |
containsValue() | Checks whether a value exists |
size() | Returns the number of entries |
isEmpty() | Checks whether the Map is empty |
clear() | Removes all entries |
keySet() | Returns all keys |
values() | Returns all values |
entrySet() | Returns all key-value pairs |
Example of Map Methods
import java.util.*; public class MapMethods { public static void main(String[] args) { Map<Integer, String> students = new HashMap<>(); students.put(101, "Anu"); students.put(102, "Ram"); students.put(103, "Sita"); System.out.println(students.get(101)); System.out.println(students.containsKey(102)); students.remove(103); System.out.println(students); System.out.println(students.size()); } }
Output
Anu true {101=Anu, 102=Ram} 2
Types of Map
1. HashMap
HashMap stores key-value pairs and does not guarantee any particular order.
Map<Integer, String> map = new HashMap<>(); map.put(1, "Apple"); map.put(2, "Mango"); map.put(3, "Banana"); System.out.println(map);
2. LinkedHashMap
LinkedHashMap maintains the insertion order of entries.
Map<Integer, String> map = new LinkedHashMap<>(); map.put(1, "Apple"); map.put(2, "Mango"); map.put(3, "Banana"); System.out.println(map);
Output
{1=Apple, 2=Mango, 3=Banana}
3. TreeMap
TreeMap stores entries according to the sorted order of keys.
Map<Integer, String> map = new TreeMap<>(); map.put(3, "Banana"); map.put(1, "Apple"); map.put(2, "Mango"); System.out.println(map);
Output
{1=Apple, 2=Mango, 3=Banana}
Iterating Through a Map
The entrySet() method can be used to access both keys and values.
import java.util.*; public class MapLoop { public static void main(String[] args) { Map<Integer, String> students = new HashMap<>(); students.put(101, "Anu"); students.put(102, "Ram"); students.put(103, "Sita"); for (Map.Entry<Integer, String> entry : students.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } } }
Output
101 : Anu 102 : Ram 103 : Sita
Map vs List vs Set
| Feature | Map | List | Set |
|---|---|---|---|
| Stores | Key-value pairs | Elements | Unique elements |
| Duplicate keys/elements | Keys: No | Yes | No |
| Index-based access | No | Yes | No |
| Main purpose | Key-value data | Ordered data | Unique data |
| Examples | HashMap, TreeMap | ArrayList | HashSet |
Real-Life Examples
Maps are useful when one piece of information is associated with another.
Examples:
- Student ID → Student Name
- Employee ID → Employee Name
- Product ID → Product Price
- Country → Capital
- Username → Password
- Word → Meaning
Example:
Student ID → Name -----------------101 → Anu 102 → Ram 103 → Sita
Advantages of Map
- Stores data as key-value pairs.
- Provides fast access using keys.
- Prevents duplicate keys.
- Allows duplicate values.
- Supports adding, updating, and removing entries.
- Different implementations provide different ordering options.
Key Points
Mapis an interface in Java.- It belongs to the
java.utilpackage. - Data is stored as key-value pairs.
- Keys must be unique.
- Values can be duplicated.
HashMapdoes not guarantee order.LinkedHashMapmaintains insertion order.TreeMapsorts entries by keys.- Common methods include
put(),get(),remove(), andcontainsKey().
Conclusion
A Map is an important Java collection used to store and retrieve data using key-value pairs. It is especially useful when each value needs a unique identifier, such as a student ID, product ID, or employee ID.