EnumSet and EnumMap

EnumSet

Most of the time when we need to create a collection of enum constants we end up writing code similar to mentioned below 
public class EnumExample {
	enum State{
		NEW,RUNNING,DEAD;
	}
	public static void main(String[] args) {
		Set<State> states = new HashSet();
		states.add(State.NEW);
		states.add(State.RUNNING);
		states.add(State.DEAD);
	}
}

Now the question is, Is there any other way around we can create a collection of enumeration so that it is optimized and faster as compared to the one we created above. Since Enum constants are unique and have pre-defined length, and you cannot add new enum constants at runtime; it allows Sun to provide highly optimized implementation of Set to store the collection of enumeration.

According to Java docs,
A specialized Set implementation for use with enum types. All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created. Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if the specified collection is also an enum set.

So if we re-write the above code using EnumSet the code look like

public class EnumExample {
	enum State{
		NEW,RUNNING,DEAD;
	}
	public static void main(String[] args) {
		Set<State> states = EnumSet.of(State.NEW, State.RUNNING, State.DEAD);
	}
}

Let us have a look on the advantages of using EnumSet over HashSet to store enumeration.
EnumSet  provides methods which simplify and the construction  of collection. Moreover, all the basic operations defined in EnumSet class execute in constant time. Operations like addAll() and removeAll() also execute in constant time.

EnumSet provides static methods to create enum set based on specified criteria, Some of the methods defined in EnumSet class and their usage is described below

public class EnumExample {
	enum State{
		NEW,RUNNING,DEAD;
	}
	public static void main(String[] args) {
		// of() method is used to create an enum and set pecified element
		EnumSet<State> states = EnumSet.of(State.NEW);
		
		//allof() method creates an enum set containing all of the elements in the specified element type
		EnumSet<State> allStates = EnumSet.allOf(State.class);
		System.out.println("AllState Method output :" +allStates);
		
		//complementOf() method creates enum set containing elements except the specified set in original Enum
		EnumSet<State> complement = EnumSet.complementOf(states);
		System.out.println("Complement Method output :" +complement);
		
		//range() method creates enum set containing elements between two endpoints
		EnumSet<State> range = EnumSet.range(State.NEW, State.DEAD);
		System.out.println("Range method output : "+range);		
	}
}

The output of above code snippet is

AllState Method output :[NEW, RUNNING, DEAD]
Complement Method output :[RUNNING, DEAD]
Range method output : [NEW, RUNNING, DEAD]

EnumMap

In some situations, developer requires creating Map based on the enums used as Key and some values corresponding to it.

For example

public class EnumExample {
	enum State{
		NEW,RUNNING,DEAD;
	}
	public static void main(String[] args) {
	HashMap<State, String> stateMap = new HashMap<State, String>();
		
        stateMap.put(State.NEW, "Thread is created");
        stateMap.put(State.RUNNING, "Thread is running");
        stateMap.put(State.DEAD, "Thread is dead");
	}
}

Rather than using HashMap java has special Map implementation EnumMap which is highly optimized and provides better performance than HashMap.

According to Java docs,

A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.

Let us implement above code using EnumMap

public class EnumExample {
	enum State{
		NEW,RUNNING,DEAD;
	}
	public static void main(String[] args) {
	EnumMap<State, String>  stateMap = new EnumMap<State, String>(State.class);
		
        stateMap.put(State.NEW, "Thread is created");
        stateMap.put(State.RUNNING, "Thread is running");
        stateMap.put(State.DEAD, "Thread is dead");

	}
}
Article tagged as
Author
Author: Amit Gupta
Published On: 05/09/2015
Last revised On: 13/11/2015
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media