Java EnumMap

EnumMap is a class which is available in java.util package which provides provides a map implementation for elements of an enum. In EnumMap, enum elements are used as keys.

EnumMap Syntax:
                        
                            enum Size {
                                SMALL, MEDIUM, LARGE, EXTRALARGE
                            }
                            
                            EnumMap<Size, Integer> sizes = new EnumMap<>(Size.class);
                        
                    
Basic Operations on Java EnumMap
  1. 1. Insert Elements to EnumMap
  2. 2. Access EnumMap Elements
  3. 3. Removed EnumMap Elements
  4. 4. Replace EnumMap Elements
Methods of EnumMap
Method Name Description
put() inserts the specified key/value mapping to the enumMap.
putAll() inserts all the entries from the specified map to this map.
entrySet() returns a set of all the keys/values mapping (entry) of an enum map.
keySet() returns a set of all the keys of the enum map.
values() returns a set of all the values of the enum map.
get() The get() method returns the value associated with the specified key. It returns null if the specified key is not found.
remove(key) returns and removes the entry associated with the specified key from the map.
remove(key, value) removes the entry from the map only if the specified key mapped to be the specified value and return a boolean value.
replace(key, value) replaces the value associated with the specified key by the new value.
replace(key, old, new) replaces the old value with the new value only if the old value is already associated with the specified key.
replaceAll(function) replaces each value of the map with the result of the specified function.
clone() Creates a copy of the EnumMap.
containsKey() Searches the EnumMap for the specified key and returns a boolean result.
containsValue() Searches the EnumMap for the specified value and returns a boolean result.
size() returns the size of the map.
isEmpty() checks if the map is empty and returns a boolean value.
1. Insert Elements to EnumMap
                        
                            import java.util.EnumMap;

                            class Main {

                                enum Size {
                                    SMALL, MEDIUM, LARGE, EXTRALARGE
                                }
                                public static void main(String[] args) {

                                    // Creating an EnumMap of the Size enum
                                    EnumMap<Size, Integer> sizes1 = new EnumMap<>(Size.class);

                                    // Using the put() Method
                                    sizes1.put(Size.SMALL, 28);
                                    sizes1.put(Size.MEDIUM, 32);
                                    System.out.println("EnumMap1: " + sizes1);

                                    EnumMap<Size, Integer> sizes2 = new EnumMap<>(Size.class);

                                    // Using the putAll() Method
                                    sizes2.putAll(sizes1);
                                    sizes2.put(Size.LARGE, 36);
                                    System.out.println("EnumMap2: " + sizes2);
                                }
                            }
                        
                    
2. Access EnumMap Elements
                        
                            import java.util.EnumMap;

                            class Main {

                                enum Size {
                                    SMALL, MEDIUM, LARGE, EXTRALARGE
                                }
                                public static void main(String[] args) {

                                    // Creating an EnumMap of the Size enum
                                    EnumMap<Size, Integer> sizes = new EnumMap<>(Size.class);
                                    sizes.put(Size.SMALL, 28);
                                    sizes.put(Size.MEDIUM, 32);
                                    sizes.put(Size.LARGE, 36);
                                    sizes.put(Size.EXTRALARGE, 40);
                                    System.out.println("EnumMap: " + sizes);

                                    // Using the entrySet() Method
                                    System.out.println("Key/Value mappings: " + sizes.entrySet());

                                    // Using the keySet() Method
                                    System.out.println("Keys: " + sizes.keySet());

                                    // Using the values() Method
                                    System.out.println("Values: " + sizes.values());
                                    
                                    // Using the get() Method
                                    int value = sizes.get(Size.MEDIUM);
                                    System.out.println("Value of MEDIUM: " + value);
                                }
                            }
                        
                    
3. Remove EnumMap Elements
                        
                            import java.util.EnumMap;

                            class Main {

                                enum Size {
                                    SMALL, MEDIUM, LARGE, EXTRALARGE
                                }
                                public static void main(String[] args) {

                                    // Creating an EnumMap of the Size enum
                                    EnumMap<Size, Integer> sizes = new EnumMap<>(Size.class);
                                    sizes.put(Size.SMALL, 28);
                                    sizes.put(Size.MEDIUM, 32);
                                    sizes.put(Size.LARGE, 36);
                                    sizes.put(Size.EXTRALARGE, 40);
                                    System.out.println("EnumMap: " + sizes);

                                    // Using the remove() Method
                                    int value = sizes.remove(Size.MEDIUM);
                                    System.out.println("Removed Value: " + value);

                                    boolean result = sizes.remove(Size.SMALL, 28);
                                    System.out.println("Is the entry {SMALL=28} removed? " + result);

                                    System.out.println("Updated EnumMap: " + sizes);
                                }
                            }
                        
                    
4. Replace EnumMap Elements
                        
                            import java.util.EnumMap;

                            class Main {

                                enum Size {
                                    SMALL, MEDIUM, LARGE, EXTRALARGE
                                }
                                public static void main(String[] args) {

                                    // Creating an EnumMap of the Size enum
                                    EnumMap<Size, Integer> sizes = new EnumMap<>(Size.class);
                                    sizes.put(Size.SMALL, 28);
                                    sizes.put(Size.MEDIUM, 32);
                                    sizes.put(Size.LARGE, 36);
                                    sizes.put(Size.EXTRALARGE, 40);
                                    System.out.println("EnumMap: " + sizes);

                                    // Using the replace() Method
                                    sizes.replace(Size.MEDIUM, 30);
                                    sizes.replace(Size.LARGE, 36, 34);
                                    System.out.println("EnumMap using replace(): " + sizes);

                                    // Using the replaceAll() Method
                                    sizes.replaceAll((key, oldValue) -> oldValue + 3);
                                    System.out.println("EnumMap using replaceAll(): " + sizes);
                                }
                            }
                        
                    
EnumSet Vs. EnumMap

Both the EnumSet and EnumMap class provides data structures to store enum values. However, there exist some major differences between them.

  1. - Enum set is represented internally as a sequence of bits, whereas the enum map is represented internally as arrays.
  2. - Enum set is created using its predefined methods like allOf(), noneOf(), of(), etc. However, an enum map is created using its constructor.