Java 10 How to create and use enum

Working with enums in Java 10 you have many options of creating and using enums. Which one to use depends on your needs:

More info on enums from Java docs:

create simple enum and get ordinal

The simple enum can be created in this way(there no change with previous versions):

public enum PopularLanguges {
    JAVA,
    JAVASCRIPT,
    PYTHON,
    SQL,
    C
}

and how to access it's information. Since the default constructor has ordinal information you can access it and use it if needed:

//access one element
System.out.println(PopularLanguges.PYTHON);

//access all
for (PopularLanguges lang : PopularLanguges.values()) {
    System.out.println("value: " + lang + "ordinal: " + lang.ordinal());
}

result:

PYTHON
ordinal: 0, value: JAVA
ordinal: 1, value: JAVASCRIPT
ordinal: 2, value: PYTHON
ordinal: 3, value: SQL
ordinal: 4, value: C

create enum with code field

You may want to define the key of the enum explicitly. You can use int, String or something else by adding instance field and overriding/ overloading the default contructor:

public enum FancyLanguges {
    GO(10),
    HASKELL(11),
    GROOVY(12),
    RUBY(13),
    COBOL(14);
    private int lang_id;
    FancyLanguges(int lang_id) {
        this.lang_id = lang_id;
    }
    public int lang_id() {
        return lang_id;
    }
}

and how to access it's information. Since the default constructor has ordinal information you can access it and use it if needed:

//access one element
System.out.println(FancyLanguges.GROOVY.lang_id());

//access all elements
for (FancyLanguges lang : FancyLanguges.values()) {
    System.out.println("ordinal: " + lang.ordinal() + ", key: " + lang.lang_id + ", value: " + lang);
}

result:

12
ordinal: 0, key: 10, value: GO
ordinal: 1, key: 11, value: HASKELL
ordinal: 2, key: 12, value: GROOVY
ordinal: 3, key: 13, value: RUBY
ordinal: 4, key: 14, value: COBOL

Iterate over enum by FOR loop

This is one of the oldest and very useful ways ot interation in Java. It's my prefereble way due to it's simplicity:

//simple enum
for (PopularLanguges lang : PopularLanguges.values()) {
    System.out.println("value: " + lang + "ordinal: " + lang.ordinal());
}

//enum with instance field
for (FancyLanguges lang : FancyLanguges.values()) {
    System.out.println("ordinal: " + lang.ordinal() + ", key: " + lang.lang_id + ", value: " + lang);
}

above you can see the results of it.

Iterate with EnumSet and forEach

Another simple and readable way is to use EnumSet and forEach:

EnumSet.allOf(PopularLanguges.class)
        .forEach(day -> System.out.println(day));

it's the best way for iteration of enums in java 8 according to my personal experience.

Iterate enum with Stream

New way is looping over enum with Streams. You can do some extra operations like filtering the values of enum:

Before using stream you need to add stream method to your enum:

For example the simple enum would be

public enum PopularLanguges {
    JAVA,
    JAVASCRIPT,
    PYTHON,
    SQL,
    C;
    public static Stream<PopularLanguges> stream() {
        return Arrays.stream(PopularLanguges.values());
    }
}

And for the enum with instance field you need to add just this:

```java
public static Stream<FancyLanguges> stream() {
    return Arrays.stream(FancyLanguges.values());
}
//using stream to iterate over enum
PopularLanguges.stream().forEach(System.out::println);

//filtering elements of enum with Streams
FancyLanguges.stream()
                .filter(d -> d.lang_id() > 11)
                .forEach(System.out::println);

result:

JAVA
JAVASCRIPT
PYTHON
SQL
C

GROOVY
RUBY
COBOL

All examples and filter enum values

import java.util.Arrays;
import java.util.EnumSet;
import java.util.stream.Stream;

public class EnumTest {

    public static void main(String[] args) {

        //
        // Iterate over simple enum
        //
        for (PopularLanguges lang : PopularLanguges.values()) {
            System.out.println("ordinal: " + lang.ordinal()+ ", value: " + lang );
        }
        //
        // access simple enum
        //
        System.out.println(PopularLanguges.PYTHON);


        //
        // Iterate over complex enum
        //
        for (FancyLanguges lang : FancyLanguges.values()) {
            System.out.println("ordinal: " + lang.ordinal() + ", key: " + lang.lang_id + ", value: " + lang);
        }
        //
        // access complex enum
        //
        System.out.println(FancyLanguges.GROOVY.lang_id());

        //
        // iterate with streams
        //
        PopularLanguges.stream().forEach(System.out::println);
        FancyLanguges.stream()
                .filter(d -> d.lang_id() > 11)
                .forEach(System.out::println);

        //
        // iterate with EnumSet and forEach
        //
        EnumSet.allOf(PopularLanguges.class)
                .forEach(lang -> System.out.println(lang));

        EnumSet.allOf(FancyLanguges.class)
                .forEach(lang -> System.out.println(lang));
    }


    //
    // Simple enum
    //
    public enum PopularLanguges {
        JAVA,
        JAVASCRIPT,
        PYTHON,
        SQL,
        C;

        public static Stream<PopularLanguges> stream() {
            return Arrays.stream(PopularLanguges.values());
        }
    }

    public enum FancyLanguges {
        GO(10),
        HASKELL(11),
        GROOVY(12),
        RUBY(13),
        COBOL(14);

        private int lang_id;

        FancyLanguges(int lang_id) {
            this.lang_id = lang_id;
        }

        public int lang_id() {
            return lang_id;
        }

        public static Stream<FancyLanguges> stream() {
            return Arrays.stream(FancyLanguges.values());
        }
    }
}

Related Article