Example 1: Loop through enum using forEach loop
Output 1
Access each enum constants SMALL, MEDIUM, LARGE, EXTRALARGE,
In the above example, we have an enum named Size. Notice the expression,
Size.values()
Here, the values() method converts the enum constants in an array of the Size type. We then used the forEach loop to access each element of the enum.
Example 2: Loop through enum using EnumSet Class
Output
Elements of EnumSet: SMALL, MEDIUM, LARGE, EXTRALARGE,
Here, we have used the allOf() method to create an EnumSet class from the enum Size. We then access each element of the enumset class using theĀ forEach loop.