2

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]);
        }
    }
Lightsout
  • 3,454
  • 2
  • 36
  • 65
urmalp
  • 382
  • 2
  • 4
  • 20

3 Answers3

2

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"));
Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
1

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!

Devarsh Desai
  • 5,984
  • 3
  • 19
  • 21
  • 4
    Or the OP could build a map within their enum - there's no need for every lookup to be O(n). Although unless the enum has a lot of values, that's not much of an issue... – Jon Skeet Jun 27 '15 at 06:09
  • 1
    @JonSkeet, 1) I am a HUGE fan of your contributions on SO. +1, You're correct. Not too familiar with the details of that implementation but will provide OP with the following links (http://www.canoo.com/blog/2010/09/24/beautiful-java-enums/) (http://javarevisited.blogspot.com/2012/09/what-is-enummap-in-java-example-tutorial.html) thank you for your time! – Devarsh Desai Jun 27 '15 at 06:16
  • 1
    Worth mentioning -- I believe at least some code in the JDK internally generates a cached map -- the generated `valueOf` source, I believe, or `Enum.valueOf(MyEnum.class, name)` – Louis Wasserman Jun 27 '15 at 07:32
  • 1
    See [this answer](http://stackoverflow.com/a/7901873/1798593) for sample code for building a map. – Patricia Shanahan Jun 27 '15 at 13:26
1

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]);
    }
}
Community
  • 1
  • 1
urmalp
  • 382
  • 2
  • 4
  • 20