Skip to content
Related Articles

Related Articles

Iterating over Enum Values in Java
  • Last Updated : 14 Jun, 2018

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :