Alexandre Bolduc Alexandre Bolduc -4 years ago 116
Java Question

Iterate through an EnumSet in java?

When I create an EnumSet and send it via a function, I find myself unable to reach the Enum values set inside the EnumSet. All I can do is compare it to the original set and check if it is present. I do not want that since that forces me to waste lines of code and forces me to reach out for the Original Enum class every single time.

for(Action a : Action.values())
{
if(stateCommands.contains(a))
{
System.out.println(a.getCommand() + a.getDescription());
}

}


I want to iterate through stateCommands and be able to see its content.

How should I proceed with this ?

Answer Source
for(StateCommand command : stateCommands) {
   // do whatever
}

Just like any other Set. Or, if you need to do something more sophisticated, just use the stateCommands.iterator().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download