for(ConnectionParameter item : ConnectionParameter.values())
How to iterate on the field of an enum in Java? [duplicate]
-
02-09-2022 - |
Question
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
Solution
OTHER TIPS
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:
You can use the values()
method like this:
for (ConnectionParameter connectionParameter : ConnectionParameter.values()) {
// do somethign with `connectionParameter` here
}
Every enum
has this values()
method.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow