0

I have this Java enum:

public enum ConnectionParameter {
    state,
    host,
    port,
    secure,
    username,
    password
}

that contains the fields of an XML file on which I have to work using XPATH (but this is not important at this time)

I have to iterate on all this enum fields to do an operation for each one.

How can I do it?

Tnx

Andrea

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

3 Answers3

6
for(ConnectionParameter item : ConnectionParameter.values())
Paul Lo
  • 6,032
  • 6
  • 31
  • 36
2

You can use ConnectionParameter.values() that returns you an array with all the constants defined in the enum. Example:

for (ConnectionParameter c :ConnectionParameter.values()) {
   System.out.println(c.printableName());
}

More info:

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
1

You can use the values() method like this:

for (ConnectionParameter connectionParameter : ConnectionParameter.values()) {
   // do somethign with `connectionParameter` here
}

Every enum has this values() method.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207