# What is an EnumMap in Java?

This article will explain what an enumMap is, and how to use it 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`

```java
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.

```java
    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

```java
System.out.println("Printing all keys in the map " + colours.keySet());
```

The result is:

```java
Printing all keys in the map [RED, BLUE, BLACK, GREEN]
```

`entrySet()`

It prints the keys and the values in the map

```java
System.out.println("Printing the keys and the values in the map " + colours.entrySet());
```

The result is:

```java
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

```java
System.out.println("Printing the values in the map " + colours.values());
```

The result is:

```java
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.

```java
System.out.println("Retrieve the value from the enumMap " + colours.get(Colour.RED));
```

The result is:

```java
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

```java
System.out.println("Removing the key: " + colours.remove(Colour.GREEN));
```

The result is:

```java
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.

```java
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:

```java
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.

```java
colours.replace(Colour.RED, 5);

System.out.println("The new enum now has: " + colours);
```

Result is:

```java
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.

```java
colours.replace(Colour.BLUE, 2, 4);

System.out.println("The new enum now has: " + colours);
```

The result is:

```java
The new enum now has: {RED=5, BLUE=4}
```

`replaceAll()`

It replaces the value of each element with the result of the function.

```java
      colours.replaceAll((key, oldValue) -> oldValue + 7);

        System.out.println("EnumMap using replaceAll(): " + colours);
```

The result is:

```java
EnumMap using replaceAll(): {RED=12, BLUE=11}
```

A few notes about enumMaps:

* EnumMaps maintains 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](https://www.linkedin.com/in/maddalena-o-bb2372020/) and [Twitter](https://twitter.com/Maddy_Onyehara).

Until next time! 🙋🏾‍♀️

***ADDITIONAL RESOURCES***

* [JavaDoc: EnumMap](https://docs.oracle.com/javase/7/docs/api/java/util/EnumMap.html)
    
* [Baeldung: A Guide to EnamMap](https://www.baeldung.com/java-enum-map)
    
* [Ritesh Panigrahi: Complete Guide to Java Enum](https://riteshpanigrahi.hashnode.dev/complete-guide-to-java-enum)
