Find if present in enum without iterating?

8.6k views Asked by At

trying to find an element by its constant parameter/field value(i.e. value0/value1/value2) without iterarting; is there a way? found guava utility method which works for enum constant name(i.e. CONSTANT0/CONSTANT1/CONSTANT2) and not parameter/field value to that name.

    import com.google.common.base.Enums;

    enum Enum {
        CONSTANT0("value0"), CONSTANT1("value1"), CONSTANT2("value2");
        private final String value;

        Enum(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

    public class Driver {
        public static void main(String[] args) {
            // prints when input is value0
            for (Enum constant : Enum.values()) {
                if (constant.getValue().equalsIgnoreCase(args[0])) {
                    System.out
                            .println("vanilla version works with value i.e. java Driver value0"
                                    + args[0]);
                }
            }
            // prints when input is CONSTANT0
            if (Enums.getIfPresent(Enum.class, args[0]).isPresent())
                System.out
                        .println("guava version works with constant i.e. java Driver CONSTANT0"
                                + args[0]);
        }
    }
3

There are 3 answers

4
Devarsh Desai On

No.

Enums were designed and introduced in Java 1.5 in order to allow developers to enumerate their constants in a cleaner way. If you look at the Enum.java source, you won't see any easy look-up method similar to that of a hash-map. For code-clarity, if you want to check if your enum contains a value, without having to iterate at every use-case, you should create a static method which iterates over the contents of your enum.

Please let me know if you have any questions!

0
urmalp On

based on suggestions by @Jon Skeet @Patricia Shanahan @Devarsh Desai @ajb

import java.util.EnumMap;
import java.util.Map;

import com.google.common.base.Enums;

enum Enum {
    CONSTANT0("value0"), CONSTANT1("value1"), CONSTANT2("value2");
    private final String value;
    static final Map<Enum, String> enumMap = new EnumMap<Enum, String>(
            Enum.class);

    static {
        for (Enum e : Enum.values())
            enumMap.put(e, e.getValue());
    }

    private Enum(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

public class Driver {
    public static void main(String[] args) {
        // prints when input is value0
        for (Enum constant : Enum.values()) {
            if (constant.getValue().equalsIgnoreCase(args[0])) {
                System.out
                        .println("vanilla version works with value i.e. java Driver value0 "
                                + args[0]);
            }
        }
        // prints when input is CONSTANT0
        if (Enums.getIfPresent(Enum.class, args[0]).isPresent())
            System.out
                    .println("guava version works with constant i.e. java Driver CONSTANT0 "
                            + args[0]);
        // prints when input is value0
        if (Enum.enumMap.containsValue(args[0]))
            System.out
                    .println("static map version works with value i.e. java Driver value0 "
                            + args[0]);
    }
}
0
Fritz Duchardt On

In absence of an elegant proprietary or library function, I suggest you go Java 8:

Stream.of(Enum.values()).anyMatch(e -> e.getValue().equals("value0"));