Introduction
In Java, Mapping means storing data in key–value pairs using the Map interface in the Java Collection Framework.
A key is used to identify a value. When we provide the key, we can easily retrieve the corresponding value.
Example:
Key → Student ID
Value → Student Name
So, when we give the student ID, we can get the student name easily.
Map Interface in Java
In Java, mapping is implemented using the Map interface.
Important Rules
Key
Duplicate keys are not allowed
Value
Duplicate values are allowed
Example:.
Key, Value
101, Kumar
102, Yogesh
103, Hari
Null Values
Only one null key is allowed (in HashMap)
Multiple null values are allowed
Common Classes in Map Interface
The most commonly used classes are:
- HashMap
- LinkedHashMap
- TreeMap****
HashMap in Java
HashMap is the most commonly used class in the Map interface.
Syntax
HashMap map = new HashMap<>();
Example:
import java.util.HashMap;
public class Sample {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(101, "Kumar");
map.put(102, "Yogesh");
map.put(103, "Hari");
System.out.println(map);
}
}
Output
{101=Kumar, 102=Yogesh, 103=Hari}
Important HashMap Methods
1. put() Method
Used to add key-value pairs.
map.put(101, "Kumar");
map.put(102, "Yogesh");
map.put(103, "Hari");
2. get() Method
Used to retrieve a value using the key.
Example:
map.put(101, "Kumar");
map.put(102, "Yogesh");
map.put(103, "Hari");
System.out.println(map.get(101));
Output
Kumar
3. remove() Method
Used to remove an element from the map.
Example:
map.put(101, "Kumar");
map.put(102, "Yogesh");
map.put(103, "Hari");
map.remove(101);
System.out.println(map);
Output
{102=Yogesh, 103=Hari}
You can also remove using key and value.
map.remove(101, "Kumar");
System.out.println(map);
4. containsKey() Method
Checks whether the key exists in the map.
Example:
map.put(101, "Kumar");
map.put(102, "Yogesh");
map.put(103, "Hari");
System.out.println(map.containsKey(103));
Output:
True
5. containsValue() Method
Checks whether the value exists in the map.
Example:
map.put(101, "Kumar");
map.put(102, "Yogesh");
map.put(103, "Hari");
System.out.println(map.containsValue("Hari"));
Output:
True
6. size() Method
Returns the number of elements in the map.
Example:
map.put(101, "Kumar");
map.put(102, "Yogesh");
map.put(103, "Hari");
System.out.println(map.size());
Output:
3
7. isEmpty() Method
Checks whether the map is empty or not.
Example:
map.put(101, "Kumar");
map.put(102, "Yogesh");
map.put(103, "Hari");
System.out.println(map.isEmpty());
Output:
False
8. clear() Method
Removes all elements from the map.
Example:
map.put(101, "Kumar");
map.put(102, "Yogesh");
map.put(103, "Hari");
map.clear();
System.out.println(map);
Output:
{}
Conclusion:
The Map Interface in Java is used to store data in key–value pairs. It allows quick retrieval of values using keys.
Among the different implementations:
HashMap → Fast and widely used
LinkedHashMap → Maintains insertion order
TreeMap → Stores keys in sorted order
Top comments (0)