The Map
interface of the Java collections framework provides the functionality of the map data structure.
In Java, elements of Map
are stored in key/value pairs. Keys are unique values associated with individual Values.
A map cannot contain duplicate keys. And, each key is associated with a single value.
We can access and modify values using the keys associated with them.
In the above diagram, we have values: United States, Brazil, and Spain. And we have corresponding keys: us, br, and es.
Now, we can access those values using their corresponding keys.
Note: The Map
interface maintains 3 different sets:
Hence we can access keys, values, and associations individually.
Since Map
is an interface, we cannot create objects from it.
In order to use functionalities of the Map
interface, we can use these classes:
These classes are defined in the collections framework and implement the Map
interface.
Java Map Subclasses
The Map
interface is also extended by these subinterfaces:
Java Map Subinterfaces
In Java, we must import the java.util.Map
package in order to use Map
. Once we import the package, here's how we can create a map.
// Map implementation using HashMap
Map<Key, Value> numbers = new HashMap<>();
In the above code, we have created a Map
named numbers. We have used the HashMap
class to implement the Map
interface.
Here,
The Map
interface includes all the methods of the Collection
interface. It is because Collection
is a super interface of Map
.
Besides methods available in the Collection
interface, the Map
interface also includes the following methods:
null
.1. Implementing HashMap Class
import java.util.Map;
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// Creating a map using the HashMap
Map<String, Integer> numbers = new HashMap<>();
// Insert elements to the map
numbers.put("One", 1);
numbers.put("Two", 2);
System.out.println("Map: " + numbers);
// Access keys of the map
System.out.println("Keys: " + numbers.keySet());
// Access values of the map
System.out.println("Values: " + numbers.values());
// Access entries of the map
System.out.println("Entries: " + numbers.entrySet());
// Remove Elements from the map
int value = numbers.remove("Two");
System.out.println("Removed Value: " + value);
}
}
Output
Map: {One=1, Two=2}
Keys: [One, Two]
Values: [1, 2]
Entries: [One=1, Two=2]
Removed Value: 2
2. Implementing TreeMap Class
import java.util.Map;
import java.util.TreeMap;
class Main {
public static void main(String[] args) {
// Creating Map using TreeMap
Map<String, Integer> values = new TreeMap<>();
// Insert elements to map
values.put("Second", 2);
values.put("First", 1);
System.out.println("Map using TreeMap: " + values);
// Replacing the values
values.replace("First", 11);
values.replace("Second", 22);
System.out.println("New Map: " + values);
// Remove elements from the map
int removedValue = values.remove("First");
System.out.println("Removed Value: " + removedValue);
}
}
Output
Map using TreeMap: {First=1, Second=2}
New Map: {First=11, Second=22}
Removed Value: 11