The Enum class is the common base class of all Java language enumeration types.
Let us see an example to iterate over enum values using for loop −
public class Demo { public enum Vehicle { CAR, BUS, BIKE } public static void main(String[] args) { for (Vehicle v : Vehicle.values()) System.out.println(v); } }
CAR BUS BIKE
Now let’s look at another example, using for each loop to iterate over enumeration values:
import java.util.stream.Stream; public class Demo { public enum Work { TABLE, CHAIR, NOTEPAD, PEN, LAPTOP } public static void main(String[] args) { Stream.of(Work.values()).forEach(System.out::println); } }
TABLE CHAIR NOTEPAD PEN LAPTOP
The above is the detailed content of Iterate over enum values in Java. For more information, please follow other related articles on the PHP Chinese website!