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 ?
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()
.
If you leave the EnumSet generic, the following will NOT work:
public void StateLoop(EnumSet stateCommands){
//won't work b/c stateCommands is treated as a generic Object
for(StateCommand command : stateCommands) {
// do whatever
}
}
However, if a Type is provided for the EnumSet it will work fine:
public void StateLoop(EnumSet<StateCommand> stateCommands){
for(StateCommand command : stateCommands) {
// do whatever
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With