Iterating over Enum Values in Java

Enum in Java is a data type that contains 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 on how to declare an enum:

// 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));
    

    Below program illustrate the iteration over enums using forEach():

    // 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 the 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:

    // 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.

    Below program illustrate the iteration over enum using java.util.stream:

    // 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




I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :
Article Tags :
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.