Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaIterating over Enum Values in Java

Iterating over Enum Values in Java

Enum in Java is a data type that contains a fixed set of constants. A Java enum type is a special kind of Java class. An enum can contain constants, methods, etc.

Given below is a demonstration of how to declare an enum:  

Java




// Demonstrates the declaration of enum
public enum season {
    Spring,
    Summer,
    Monsoon,
    Autumn,
    Winter
}


 

Iterate over Enum Values: There are many ways to iterate over enum values:  

  1. Iterate Using forEach()
  2. Iterate Using for Loop
  3. Iterate Using java.util.stream 

Iterate Using forEach(): The forEach() method works on list or set. For using forEach() method, convert enum into list or set. 

Conversion of enum to set and applying forEach has been shown below: 

EnumSet.allOf(seasons.class)
            .forEach(season -> System.out.println(season));

Conversion of enum to list and applying forEach has been shown below: 

Arrays.asList(seasons.values()).
                forEach(season -> System.out.println(season));

The below program illustrate the iteration over enums using forEach(): 

Java




// Program that demonstrates iteration
// over enums using forEach()
import java.util.Arrays;
import java.util.EnumSet;
 
public class GFG {
    public enum seasons {
        Spring,
        Summer,
        Monsoon,
        Autumn,
        Winter
    }
    public static void main(String[] args)
    {
 
        // Convert enum to set and apply forEach()
        EnumSet.allOf(seasons.class)
            .forEach(season -> System.out.println(season));
 
        // Convert enum to set and apply forEach()
        Arrays.asList(seasons.values())
            .forEach(season -> System.out.println(season));
    }
}


Output

Spring
Summer
Monsoon
Autumn
Winter
Spring
Summer
Monsoon
Autumn
Winter

Iterate Using for Loop: The static values() method of java.lang. Enum class gives an array of enum values. After getting an array of enum values iteration over the array can be done using for loop.

Below program illustrate the iteration over enum using for loop: 

Java




// Program that demonstrates iteration
// over enums using for loop
public class GFG {
    // enum of seasons
    public enum seasons {
        Spring,
        Summer,
        Monsoon,
        Autumn,
        Winter
    }
    public static void main(String[] args)
    {
        // iterate over enums using for loop
        for (seasons s : seasons.values()) {
            System.out.println(s);
        }
    }
}


Output

Spring
Summer
Monsoon
Autumn
Winter

Iterate Using java.util.stream: Iteration over enum can also be done using java.util.stream class. Create the stream of enum.values() using Stream.of() method by passing eum.values() at parameter to function. 

The below program illustrates the iteration over enum using java. util.stream: 

Java




// Program that demonstrates iteration
// over enums using java.util.stream
 
import java.util.stream.Stream;
 
public class GFG {
    public enum seasons {
        Spring,
        Summer,
        Monsoon,
        Autumn,
        Winter
    }
    public static void main(String[] args)
    {
        // iteration over enum using java.util.stream
        Stream.of(seasons.values())
            .forEach(System.out::println);
    }
}


Output

Spring
Summer
Monsoon
Autumn
Winter

Reference: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

RELATED ARTICLES

Most Popular

Recent Comments