

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterating 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); } }
Output
CAR BUS BIKE
Example
Let us now see another example to iterate over enum values using for each −
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); } }
Output
TABLE CHAIR NOTEPAD PEN LAPTOP
- Related Questions & Answers
- Iterating over array in Java
- Iterating over ArrayLists in Java
- Iterating over Arrays in Java
- The Iterating over Arrays in Java
- Java Program to Iterate over enum
- Enumerate over an enum in C++
- Enum in Java
- How to iterate the values in an enum in Java?
- Can we create an enum with custom values in java?
- C++ Remove an Entry Using Key from HashMap while Iterating Over It
- C++ Remove an Entry Using Value from HashMap while Iterating over It
- Enum Methods in Java
- Enum constructor in Java
- Set ENUM in MySQL for column values
- How Are MySQL ENUM values sorted?
Advertisements