Over a million developers have joined DZone.

EnumSet Efficiency

DZone's Guide to

EnumSet Efficiency

See how you can incorporate EnumSets into your code. In this example, we walk through implementing and combining roles with EnumSets.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

EnumSet is a specialized set that only takes Enum elements. Look at the signature of this class.

public abstract class EnumSet<E extends Enum<E>>
extends AbstractSet<E>
implements Cloneable, Serializable

The beauty of EnumSet is that we can perform any collection operation like (add, remove, etc. ) on Enum. As EnumSet is a collection, it provides some Static methods by which we can do union, range check, none, etc. operations.

So by using EnumSet, we can dynamically add Enum elements, remove an element, or perform union operations.

So where can we use EnumSet? Well, one case is role and operation implementation. Not only that, say from the UI, we can manipulate it and create a new role with a new operation set.

To be specific, say I have three types of roles: Self, Friend, and Guest.

As you can guess, the owner of the profile gets the Self role, friends of the owner get the Friend role, and any unknown people get the Guest role. Now our Owner wants to create a new role for a few guests — a role where they can perform Friend operations as well as guest operations.


Obviously, we can consider Bit representation for operations and check if a particular operation is valid for a role or not — the same way we can create a new role. I don’t go for that implementation, though. That will be very hard to maintain. Rather I will try to solve it through an EnumSet.

1. I create an Operation Enum where I store all the possible Operations.

package com.example.enumtest;
public enum Operation {

2. Then I create a Role Enum and associate permitted operations with those roles.

package com.example.enumtest;
import java.util.EnumSet;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections;
public enum Role {

    EnumSet<Operation> operationSet;
    Role(EnumSet<Operation> operationSet){

Look in my Role Constructor — I pass EnumSet<Operation>, and when we define the Enum element, I called a Static method on EnumSet. That will perform a union operation.

3. Now, each role has its set of operations, and we can easily print them by traversing operationSet, easily checking whether an operation is permitted:


4. Now I want to add a new Role that can perform both Friend and Guest operations.

package com.example.enumtest;
import java.util.EnumSet;
public class ProfileManager {
    public static void printOperationForCustomRole(EnumSet<Role>roles)
        EnumSet<Operation> mergeSet = EnumSet.noneOf(Operation.class);
        for(Role role: roles)
            for(Operation op : role.operationSet)

    public static void main(String[] args) {

Look how easily we can do it. Just pass Role.Friend and Role.Guest in the printOperationForCustomRole() role method and create a new Operation set that holds all the operations of Friend and Guest.

And for our output:


Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

enums ,java ,enumset ,roles

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}