Open In App

Difference Between EnumMap and EnumSet in Java

Last Updated : 23 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
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());
        }
    }
}


Output

Size of EnumMap: 4
Apple 1
Mango 2
Orange 3
Banana 4

2. 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());
        }
    }
}


Output

EnumSet : 
Monday
Tuesday

Difference between EnumMap and EnumSet

                                 EnumMap                              EnumSet
EnumMap 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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads