EnumSet

Java EnumSet Example and Efficiency

EnumSet is one of the specialized Set interface implementation for use with enum types which was introduced in Java 1.5. Many don’t know the purpose or unware of this implementation and end up in storing Enum data in to common collection classes like ArrayList and HashSet.

EnumSet example

Output

Note:

You cannot insert Null elements in to EnumSet. If you still try to insert, then you end up with NullPointerException.

EnumSet is not Synchronized

Like many other collections, it is not synchronized. In order to prevent accidental unsynchronized access by threads, it should be synchronized externally like the following

Time Complexity

Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class is good.

All the basic operations execute in constant time O(1). Though not guaranteed they are much faster when compared to HashSet counterparts. Even when you perform bulk operations it performs better and operations execute in constant time.

Related Posts

References

Leave a Reply

avatar
  Subscribe  
Notify of