5 Difference between EnumMap and EnumSet in Java

If you are curious about EnumMap and EnumSet classes in java collections framework then this article is for you. Questions related to the EnumMap and EnumSet are generally not asked in the java developer interview. That includes the question difference between EnumMap and EnumSet. But it does not mean that you avoid these classes altogether. This article will cover the difference between EnumMap and EnumSet, EnumMap example, EnumSet example and similarities between them.   

Read Also: Difference between EnumMap and HashMap in Java

Difference between EnumMap and EnumSet in Java

1. Internal representation :EnumMap internally represented as arrays.EnumSet internally represented as BitVector.

2. Permits Null : In EnumMap null keys are not permitted, however, null values are permitted.
In EnumSet null elements are not permitted.

3. Abstract class :  EnumMap is not an abstract class. While, EnumSet is an abstract class.

4. Instantiation : Since EnumMap is not an abstract class, it can be instantiated using new operator. You can not directly create instances of EnumSet as it is an abstract class. EnumSet does not have any constructor.

5. Implementation : EnumMap is a specialized Map implementation for use with enum type keys.
EnumSet is a specialized Set implementation for use with enum types.

Examples of EnumMap and EnumSet

Below are the examples of EnumMap and EnumSet. First look at the example of EnumSet

import java.util.EnumSet;
public class EnumSetExample {
enum Direction
{
NORTH, SOUTH, EAST, WEST;
};

public static void main(String args[]) {

// of() Method Example : Creates an EnumSet initially 
      // containing the specified elements

EnumSet<Direction> set1 = 
                         EnumSet.of(Direction.NORTH,Direction.SOUTH);
System.out.println("of() Example "+ set1);


// allOf Method Example : creates an EnumSet 
      // containing all the elements

EnumSet<Direction> set2 = EnumSet.allOf(Direction.class);
System.out.println("allOf() Example "+ set2);


// noneOf Method Example : creates an empty EnumSet

EnumSet<Direction> set3 = EnumSet.noneOf(Direction.class);
System.out.println("noneOf() Example "+ set3);


// complementOf Method Example : creates an
      // EnumSet not contained in the specified set

EnumSet<Direction> set4 = EnumSet.complementOf(set1);
System.out.println("complementOf() Example "+ set4);


// range Method Example : creates an EnumSet
      // containing all the elements in the range

EnumSet<Direction> set5 = 
                         EnumSet.range(Direction.SOUTH,Direction.WEST);
System.out.println("range() Example "+ set5);
}
}
 
 

Output :

of() Example [NORTH, SOUTH]
allOf() Example [NORTH, SOUTH, EAST, WEST]
noneOf() Example []
complementOf() Example [EAST, WEST]
range() Example [SOUTH, EAST, WEST]

Now, look at the example of EnumMap

import java.util.EnumMap; 

public class JavaHungry
{
public enum Fruit
{
BANANA, APPLE, ORANGE, MANGO;
}

public static void main(String args[])
{
// Java EnumMap Example
// Creating EnumMap in java with key
// as enum type STATE
EnumMap<Fruit, String> fruitMap = new
EnumMap<Fruit, String>(Fruit.class);

// Java EnumMap Another Example:
// Putting values inside EnumMap in Java
// Inserting Enum keys different from
// their natural order
fruitMap.put(Fruit.ORANGE, "Orange is delicious");
fruitMap.put(Fruit.APPLE, "Apple is delicious");
fruitMap.put(Fruit.MANGO, "Mango is delicious");
fruitMap.put(Fruit.BANANA, "Banana is delicious");

// Displaying size of EnumMap in java
System.out.println("Size of EnumMap : " +
fruitMap.size());

// Printing Java EnumMap
// Print EnumMap in natural order
// of enum keys (order on which they are declared)
System.out.println("EnumMap : " + fruitMap);

// Fetch value from EnumMap in java
System.out.println("Key : " + Fruit.MANGO +" Value: "
+ fruitMap.get(Fruit.MANGO));
}
}

Output :
Size of EnumMap : 4
EnumMap : {BANANA=Banana is delicious, APPLE=Apple is delicious, ORANGE=Orange is delicious, MANGO=Mango is delicious}
Key : MANGO Value: Mango is delicious

Similarities between EnumMap and EnumSet in Java

1. Both EnumMap and EnumSet execute basic operations in constant time.

2. Like most collection implementations, EnumMap and EnumSet are not synchronized.

3. Iterators returned by the collection views(keySet(), entrySet(), and values()) in EnumMap and iterators returned by iterator method in EnumSet both are weakly consistent.They will never throw ConcurrentModificationException.They may or may not show the effects of any modifications to the map(or set) that occur while the iteration is in progress.

4. Both EnumMap and EnumSet  were introduced in jdk 1.5 .

5. Both EnumMap and EnumSet are members of Java Collections Framework.

Recap : Difference between EnumMap and EnumSet in Java


EnumMapEnumSet
Internal RepresentationArraysBit Vector
Permits NullNull values are permittedNo
Abstract ClassNoYes
InstantiationUsing new operatorDirect instantiation is not possible
ImplementationSpecialized Map implementationSpecialized Set implementation

In this article I have demonstrated the EnumSet example, EnumMap example, similarities between EnumMap and EnumSet and difference between EnumMap and EnumSet.Please mention in the comments if you have any questions regarding EnumSet and EnumMap. I will be happy to answer.

References :
EnumSet Oracle doc
EnumMap Oracle doc

0 Comments