Difference Between EnumMap and EnumSet in Java Last Updated : 23 Nov, 2022 Comments Improve Suggest changes Like Article Like Report EnumMap and EnumSet both are the classes defined inside the java collection. In this article, we will learn the differences between EnumMap and EnumSet. EnumMap is the specialized implementation of the Map interface and the EnumSet is the specialized implementation of the Set interface. There are some differences that exist between them. So we have tried to list out the differences between EnumMap and EnumSet. 1. EnumMap: EnumMap is a specialized implementation of the Map interface for enumeration types. It implements the Map interface and extends AbstractMap in Java. EnumMap is much faster than HashMap.EnumMap class is a member of the Java Collections Framework.EnumMap is an ordered collection maintained in the natural order of their keys.All keys of each EnumMap instance must be keys of the same enum type.EnumMap doesn’t allow inserting a null key if we try to insert the null key, it will throw NullPointerException.EnumMap internally represented as arrays for better performance.Below is the implementation of the EnumMap: Java // Java program to illustrate working of EnumMap import java.util.*; class EnumMapExample { public enum Fruits { Apple, Mango, Orange, Banana; } public static void main(String[] args) { // Creating an EnumMap of the Fruits enum EnumMap<Fruits, Integer> enumMap = new EnumMap<>(Fruits.class); // Insert using put() method enumMap.put(Fruits.Apple, 1); enumMap.put(Fruits.Mango, 2); enumMap.put(Fruits.Orange, 3); enumMap.put(Fruits.Banana, 4); // Printing size of EnumMap System.out.println("Size of EnumMap: " + enumMap.size()); // Printing the EnumMap for (Map.Entry m : enumMap.entrySet()) { System.out.println(m.getKey() + " " + m.getValue()); } } } OutputSize of EnumMap: 4 Apple 1 Mango 2 Orange 3 Banana 42. EnumSet: EnumSet is a specialized implementation of the Set interface for enumeration types. It implements the Set interface and extends AbstractSet in Java. EnumSet class is a member of the Java Collections Framework and it is not synchronized.All the elements in an EnumSet must come from a single enumeration type that is specified when the set is created either explicitly or implicitly.EnumSet is much faster than HashSet.EnumSet doesn’t allow inserting null object if we try to insert the null object, it will throw NullPointerException.Below is the implementation of the EnumSet: Java // Java program to demonstrate the EnumSet import java.util.*; class enumSetExample { enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday } public static void main(String args[]) { // Creating an EnumSet EnumSet<Days> days = EnumSet.of(Days.Monday, Days.Tuesday); Iterator<Days> itr = days.iterator(); // Iterate and print elements // to the console System.out.println("EnumSet : "); while (itr.hasNext()) { System.out.println(itr.next()); } } } OutputEnumSet : Monday TuesdayDifference between EnumMap and EnumSet EnumMap EnumSetEnumMap is a specialized implementation of the Map interface for the enumeration types.EnumSet is a specialized implementation of the Set interface for the enumeration types.EnumMap is internally represented as an array.EnumSet is internally represented as a BitVector.EnumMap doesn't allow to insert the null keys, however, null values can be inserted.EnumSet doesn't allow inserting null elements.EnumMap is not an abstract class.EnumSet is an abstract class.EnumMap is not an abstract class therefore, we can create an instance of this class.EnumSet is an abstract class therefore, we can not create the instance of this class. Comment More infoAdvertise with us Next Article Difference Between EnumMap and EnumSet in Java prashant_srivastava Follow Improve Article Tags : Java Technical Scripter Difference Between Technical Scripter 2020 Java-Collections java-EnumSet Java-EnumMap +3 More Practice Tags : JavaJava-Collections Similar Reads Difference Between EnumSet and TreeSet in Java EnumSet and TreeSet both are the classes defined inside the collection framework. But there are few differences exists between them. In this article, we have tried to cover all these differences between them. 1. EnumSet: EnumSet is a specialized implementation of the Set interface for enumeration ty 3 min read Difference Between EnumMap and HashMap EnumMap and HashMap both are the classes that implement the Map interface. But there are some differences that exist between them. So we have tried to list out the differences between EnumMap and HashMap. 1. EnumMap: EnumMap is a specialized implementation of the Map interface for enumeration types. 4 min read Differences between HashMap and HashTable in Java HashMap and Hashtable store key and value pairs in a hash table. When using a Hashtable or HashMap, we specify an object that is used as a key and the value that you want to be linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored w 3 min read Difference between HashMap and IdentityHashMap in Java HashMap in java is a class that is a part of the java collection. It implements the Map interface of java. It stores the data in the form of key and value pair. The key should be unique but the value may be duplicate. If you try to insert the duplicate key, it will replace the element of the corresp 4 min read Difference between ArrayList and HashSet in Java Here are couple of differences between ArrayList and HashSet. Inheritance: Implementation: Implementation : ArrayList implements List interface while HashSet implements Set interface in Java.Internal implementation: ArrayList is backed by an Array while HashSet is backed by an HashMap.Duplicates : A 3 min read Difference Between map() And flatMap() In Java Stream In Java, the Stream interface has a map() and flatmap() methods and both have intermediate stream operation and return another stream as method output. Both of the functions map() and flatMap are used for transformation and mapping operations. map() function produces one output for one input value, 3 min read Difference between HashMap and HashSet HashSet is an implementation of Set Interface which does not allow duplicate value. The main thing is, objects that are stored in HashSet must override equals() for check for equality, and hashCode() methods for no duplicate value are stored in our set. HashMap is an implementation of Map Interface, 4 min read Difference Between List and Set in Java The List interface allows storing the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values are allowed to store. List preserves the insertion order, it allows positional access and insertion of elements. Declaration: public abstr 2 min read Difference Between ordinal() and compareTo() in Java Enum Enumerations serve the purpose of representing a group of named constants in a programming language. If we want to represent a group named constant then we should go for Enum. Every enum constant is static. Hence, we can access it by using enum Name. Example: enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNE 3 min read Difference Between keySet() and entrySet() Method in Java Map Map Interface is present in Java.util package, which provides mainly three methods KeySet(),entrySet() and values(). These methods are used to retrieve the keys of the map, key-value pairs of the map, and values of the map respectively. Since these methods are part of Map Interface, so we can use th 4 min read Like