Home > Java > javaTutorial > Iterate over enum values ​​in Java

Iterate over enum values ​​in Java

WBOY
Release: 2023-09-12 11:01:02
forward
894 people have browsed it

Iterate over enum values ​​in Java

The Enum class is the common base class of all Java language enumeration types.

Example

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);
   }
}
Copy after login

Output

CAR
BUS
BIKE
Copy after login

Example

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);
   }
}
Copy after login

Output

TABLE
CHAIR
NOTEPAD
PEN
LAPTOP
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template