Blog / April 29, 2023 / 5 mins read / By Suneet Agrawal

Map Implementation in Java

Java provides a powerful data structure called Map, which is used to store key-value pairs. A Map is a collection of unique keys and their corresponding values, and it provides efficient methods to add, remove, and access elements in constant time.

In this blog, we will discuss the implementation of the Map interface in Java and explore some of its useful methods.

What is a Map?

A map is used to store key value pairs where each key in a map will be unique.


Map Interface in Java

The Map interface is part of the java.util package and is used to store key-value pairs. The Map interface is not a subtype of the Collection interface. Therefore it behaves a bit differently from the rest of the collection types. The Map interface provides several methods to manipulate the key-value pairs. Here are some of the most commonly used methods:

  • put(key, value): Adds the specified key-value pair to the map. If the key already exists, the old value is replaced by the new value.
  • get(key): Returns the value associated with the specified key, or null if the key is not found.
  • remove(key): Removes the key-value pair with the specified key from the map, if it exists.
  • containsKey(key): Returns true if the map contains the specified key, false otherwise.
  • keySet(): Returns a Set of all the keys in the map.
  • clear(): Removes all the elements from the map.
  • values(): Returns a Collection of all the values in the map.

General-Purpose Map Implementations

HashMap Implementation

HashMap is the most commonly used implementation of the Map interface. It is an unordered collection of key-value pairs and provides constant-time performance for the basic operations, such as get and put. It uses the hash function to compute the hash code of the key, which is then used to find the bucket in which the entry should be stored.

Here is an example of how to create a HashMap and add elements to it:

// create a new HashMap
HashMap<String, Integer> myMap = new HashMap<>();

// add some key-value pairs to the map
myMap.put("apple", 1);
myMap.put("banana", 2);
myMap.put("orange", 3);

// get the value for a specific key
Integer value = myMap.get("banana");
System.out.println("The value for the key 'banana' is: " + value);

// remove a key-value pair from the map
myMap.remove("orange");

// iterate over the key-value pairs in the map
for (String key : myMap.keySet()) {
System.out.println("Key: " + key + ", Value: " + myMap.get(key));
}

TreeMap Implementation

TreeMap is a sorted implementation of the Map interface. It maintains the elements in a sorted order based on the natural ordering of the keys or a custom Comparator. It provides logarithmic time performance for the basic operations, such as get and put.

Here is an example of how to create a TreeMap and add elements to it:

Map<String, Integer> map = new TreeMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);

LinkedHashMap Implementation

LinkedHashMap is a hybrid implementation of the Map interface that maintains the elements in the order in which they were inserted. It provides constant-time performance for the basic operations, such as get and put.

Here is an example of how to create a LinkedHashMap and add elements to it:

Map<String, Integer> map = new LinkedHashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);

Special-Purpose Map Implementations

EnumMap

It is more efficient than a regular HashMap when working with Enum keys since it is implemented as an array, which provides O(1) time complexity for accessing and updating values based on an Enum key.

Here is an example of creating an EnumMap and adding elements to it:

// Create an EnumMap with Day as the key type and String as the value type
EnumMap<Day, String> activities = new EnumMap<Day, String>(Day.class);

// Add values to the EnumMap
activities.put(Day.MONDAY, "Work");
activities.put(Day.TUESDAY, "Workout");
activities.put(Day.WEDNESDAY, "Work");
activities.put(Day.THURSDAY, "Meetings");
activities.put(Day.FRIDAY, "Work");
activities.put(Day.SATURDAY, "Chores");
activities.put(Day.SUNDAY, "Relax");

// Access values in the EnumMap
String activityOnMonday = activities.get(Day.MONDAY);
System.out.println("Activity on Monday: " + activityOnMonday);

// Iterate over the values in the EnumMap
for (Day day : activities.keySet()) {
System.out.println(day + ": " + activities.get(day));
}

ConcurrentHashMap

ConcurrentHashMap is a thread-safe implementation of the Map interface that allows multiple threads to access and modify the map concurrently. It provides high concurrency and scalability, and it is designed to handle high throughput and low latency.

Here is an example of how to create a ConcurrentHashMap and add elements to it:

Map<String, Integer> map = new ConcurrentHashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);

WeakHashMap

WeakHashMap is a class in Java that implements the Map interface, similar to HashMap or TreeMap. However, unlike HashMap or TreeMap, WeakHashMap holds weak references to its keys.

In a weak reference, an object is considered eligible for garbage collection if there are no strong references to it. In the context of WeakHashMap, this means that if a key is no longer in use by any other part of the program, it can be automatically removed from the WeakHashMap. This makes WeakHashMap useful for cases where you want to associate data with objects that are only temporarily used, such as cached data or event handlers.

Conclusion

In conclusion, The Map interface in Java provides a convenient way to store key-value pairs. It has various implementations such as HashMap, TreeMap, LinkedHashMap, and Hashtable, which have their unique features and advantages. If you need a fast, unordered Map, you can use HashMap. If you need an ordered Map, you can use TreeMap or LinkedHashMap. If you need a concurrent Map, you can use ConcurrentHashMap. Finally, if you need to store key-value pairs where the keys are enum constants, you can use EnumMap.


Comments