Some days ago, I bumped into an error on Intellij. I don't remember specifically what it was. But I remember Intellij suggesting I use an "enumMap".
I knew about the existence of enums, but I wasn't aware of the existence of enumMaps.
So here is an article covering enumMaps, in Java.
What is an EnumMap?
An EnumMap is a special form of a map. A map in Java is an interface that takes a key (K) and a value (V).
In the case of an EnumMap, the key is an enum.
An EnumMap:
extends the AbstractMap.
implements Map interface.
How to create an EnumMap
To create an enumMap, we have to import the java.util.EnumMap
public class JavaEnumMap {
enum Colour {
RED,
BLUE,
BLACK,
GREEN
}
}
How to add an element into an enumMap
You can use the put() method to insert elements into an enumMap.
public static void main(String[] args) {
EnumMap<Colour, Integer> colours = new EnumMap<Colour, Integer>(Colour.class);
colours.put(Colour.RED, 1);
colours.put(Colour.BLUE, 2);
colours.put(Colour.BLACK, 3);
colours.put(Colour.GREEN, 4);
}
How to access an element on an enumMap
To access the elements of an enumMap, you can use:
keySet()
It prints all the keys on the map
System.out.println("Printing all keys in the map " + colours.keySet());
The result is:
Printing all keys in the map [RED, BLUE, BLACK, GREEN]
entrySet()
It prints the keys and the values in the map
System.out.println("Printing the keys and the values in the map " + colours.entrySet());
The result is:
Printing the keys and the values in the map [RED=1, BLUE=2, BLACK=3, GREEN=4]
values()
It prints the values in the map
System.out.println("Printing the values in the map " + colours.values());
The result is:
Printing the values in the map [1, 2, 3, 4]
How to retrieve elements from an enumMap
You can use the get() method to retrieve elements from an enumMap.
System.out.println("Retrieve the value from the enumMap " + colours.get(Colour.RED));
The result is:
Retrieve the value from the enumMap: 1
How to remove elements from an enumMap
To remove elements from an enumMap you can use:
remove(key)
You use the key of the enumMap to remove an element from an enumMap
System.out.println("Removing the key: " + colours.remove(Colour.GREEN));
The result is:
Removing the key: 4
The enumMap now has: {RED=1, BLUE=2}
*remove(key, value) *
If it exists, it removes the element from the enumMap and returns a boolean value.
System.out.println("Removing the key and the value from the enumMap: " + colours.remove(Colour.BLACK, 3));
Removing the key and the value from the enumMap: true
The enumMap now has: {RED=1, BLUE=2}
The result is:
The enumMap now has: {RED=1, BLUE=2}
How to replace an enumMap
replace(key, value)
It replaces the old value with a new value.
colours.replace(Colour.RED, 5);
System.out.println("The new enum now has: " + colours);
Result is:
The new enum now has: {RED=5, BLUE=2}
replace (key, oldValue, newValue)
This method replaces the old value with a new one only if the old value is linked with a specific key.
colours.replace(Colour.BLUE, 2, 4);
System.out.println("The new enum now has: " + colours);
The result is:
The new enum now has: {RED=5, BLUE=4}
replaceAll()
It replaces the value of each element with the result of the function.
colours.replaceAll((key, oldValue) -> oldValue + 7);
System.out.println("EnumMap using replaceAll(): " + colours);
The result is:
EnumMap using replaceAll(): {RED=12, BLUE=11}
A few notes about enumMaps:
EnumMaps maintain the natural order of their keys.
We cannot have null keys.
Internally, enumMaps are represented as arrays.
CONCLUSION
In this article, you've learned about enumMaps and its methods.
I hope you've found this article helpful.
If you enjoy my writing, please consider subscribing to my newsletter.
I'm also on LinkedIn and Twitter.
Until next time! 🙋🏾♀️
ADDITIONAL RESOURCES
Top comments (0)