What is an EnumSet in Java ?

EnumSet is a specialized implementation of Set...

Use - It is mainly used with enum types.

Single enum type - All the elements in an EnumSet must come from a single enum type when the set is created.

Bit vector - Internally, EnumSet is represented as bit vector.

Iterator - The iterator of EnumSet traverses the elements in their natural order. (It is the order in which the enum constants are declared).

Null - In an EnumSet, null elements are not permitted. If we try to insert a null element it throws NullPointerException.

Thread-safe - EnumSet is not a synchronized collection. For use in multi-threading scenarios, EnumSet should be synchronized.

Bit flags - EnumSet is a very good alternative to int based “bit flags” implementation.



You May Interest

When is UnsupportedOperationException Thrown in Java ?

What is the Difference Between Throw and Throws in Java ?

What are the Rules of Method Overloading and Method Overriding in ...

Enumeration and Iterator, Which One Has Better Performance in Jav ...

What are the Different States of a Thread in Java ?