How to Iterate over Java Enum : values() Example

Enum Values() Example
In this Java programming tutorial, we will learn how to iterate over enum in Java. Since Enums are a collection of a finite number of well-known objects, often we need to iterate over them. Enums are also final in Java and have a private constructor, which means you can not create enum instances once declared. Iteration over Enum is extremely simple, provided you know about the implicit values() method, which is a static method, provided by java.lang.Enum. Since every enum in Java extends java.lang.Enum, they all get this implicit values() method. Actually there are couple of them, e.g. valueOf(), name(), ordinal() etc. This method returns enum constants in the order they are declared in the Enum class. Even the ordinal() method returns the same order.

By the way, In the last couple of Java tutorials on enum, we have seen How to convert Enum to String in Java, Enum valueOf Example, and Enum Constructor Example.

This tutorial focuses on iteration over Enum in Java. Remember iteration, traversing or looping over enum is the same thing, so you can use this technique for any of these purposes.



How to loop over Enum in Java

How to loop over Enum in Java with ExampleIn this Java program, we have created an enum called Language, which represents programming language and their rank. Our task is to iterate through this enum and print the name, and rank of each enum instance.



Just remember that all enum in Java provides the values() method, which returns an array of enum, containing all instances. Here is a full code example of Iterating over enum :

/**
 * Java program to iterate over enum using for loop and values method.
 * values() method of enum returns all enum instances as array for iteration.
 *
 * @author java67
 */
public class EnumHowTo {

    public enum Language{
        JAVA(1), PYTHON(2), PERL(3), SCALA(4);
   
        private int rank;
       
        private Language(int rank){
            this.rank = rank;
        }
       
        public int getRank(){
            return rank;
        }
   
    };
   
    public static void main(String args[]) {

        System.out.println("Java Enum Iterate Example using for loop");
       
        for(Language pl : Language.values()){
            System.out.println( pl.name() + " : " + pl.rank);
        }
   
    } 
   
}

Output:
Java Enum Iterate Example using for loop
JAVA : 1
PYTHON : 2
PERL : 3
SCALA : 4


That's all on How to iterate over enum in Java. As I said earlier and you can see from this code example, it's simple. Don't forget the values() method, which returns an array of enum instances. To learn more about Enum in Java and their usages, see the following tutorials :


Related Java Enum Tutorials for further reading :
  1. Top 15 Java Enum Interview Questions with Answers (see here)
  2. Difference between RegularEnumSet and JumboEnumSet in Java (read here)
  3. String to Enum in Java with Example (check here)
  4. Can we use Enum in Switch Statement in Java (Yes)
  5. How to loop over Enum constants in Java (example)
  6. Learn how to use Enum in Java with a Simple Example (check here)
  7. Java tip to convert  Enum to String in Java (see tip)
  8. How to use the valueOf method of Enum (check here)
  9. What Every Java Programmer Should know about Enum (click here)
  10. 10 points about Enum in Java (see here)
  11. Can Enum have Constructor in Java (learn here)
  12. 10 Courses to learn Java for Beginners (courses)
Thanks for reading this article so far. If you find this article useful then please share it on Facebook, Twitter, and LinkedIn. 

2 comments:

  1. why you are maintaing 2 blogs...javarevisited and java67? if u can consolidate everything in one it will be useful for the readers

    ReplyDelete
  2. Couple of More articles on Java Enum :
    10 Enum Examples in Java

    15 Java Enum Interview Questions.

    Both of them provide good overview of different enum features.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.