-4
\$\begingroup\$

Write a Java method to check if a given string matches any value in a given enum.

Your method will take two parameters, one a string and one an enum, and return true if the string representation of any of the enum's elements matches the given string, or false otherwise. Note that your method should still function normally if the String parameter is null, and that you aren't allowed to use any external libraries.

Test cases:

public enum E { A, B, C; }

public enum F { X, Y, Z; }

check("a", E.A); // is false (because "A" is uppercase) 
check(null, E.A); // is false
check("C", E.A); // is true
check("X", F.X); // is true
check(null, F.X); // is false
check("y", F.X); // is false

The method has access (as a solver correctly understood) to a generic type E that has the only bound that "E extends Enum(E)".

Reference implementation (Ungolfed):

boolean <E extends Enum<E>> check(String s, E e){
    for(E ex : e.getDeclaringClass().getEnumConstants())
        if(ex.toString().equals(s)) return true;
    return false;
}

Another possible implementation (due to a solution) is (Ungolfed):

public static <E extends Enum<E>> boolean check(String s, E e){
    return java.util.stream.Stream.of(e.getDeclaringClass().getEnumConstants()).anyMatch(x->x.name().equals(s));
}

What I'm looking for is the shortest thing I could put after return (or, as a solutor did, after "(String s,E e)->)".

This is , so fewest bytes wins!

\$\endgroup\$
17
  • \$\begingroup\$ What should the method do? With the current spec, always returning true is allowed. \$\endgroup\$ Commented Dec 1, 2016 at 9:43
  • 3
    \$\begingroup\$ Single-language challenges are also discouraged here. Unless the challenge only makes sense for Java, you should allow all languages. \$\endgroup\$ Commented Dec 1, 2016 at 9:47
  • 1
    \$\begingroup\$ The task could be described in the main text more explicitly, e.g. "The method should return true if the string is equal to the string representation of any value in the enum class, and false otherwise. null strings should also result in false." Remove the "shortest code wins" part too; tips questions are not competitions. \$\endgroup\$ Commented Dec 1, 2016 at 11:37
  • 1
    \$\begingroup\$ Otherwise i can not see why E.valueOf() would be incorrect to do, or why do we need a second argument at all (as that is no different of how your check method operates). \$\endgroup\$ Commented Dec 2, 2016 at 19:54
  • 1
    \$\begingroup\$ 1. The words "given" and "enum" are both somewhat ambiguous. If you really intend for the signature of the method to be boolean c(String, E) then "given" means two different things in the first sentence of the question. Other interpretations would lead to signatures boolean c(String, Enum) or boolean c(String, Class<? extends Enum>). The comment above that you "prefer if you don't make assumptions on the class E" suggests that you want the second or third signature, but the reference implementation uses the first. 2. It's confusing to ask for return values of True and False \$\endgroup\$ Commented Jan 11, 2017 at 17:39

2 Answers 2

1
\$\begingroup\$

Java 8, 86 bytes

(String s,E e)->java.util.stream.Stream.of(E.values()).anyMatch(x->x.name().equals(s))

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ My experience with java Stream is that it works by some odd magic, but it works so I won't question it and hopefully my code doesn't fall apart. Java really isn't my thing. \$\endgroup\$ Commented Dec 2, 2016 at 20:50
  • \$\begingroup\$ Taking that you do E.values() (which implies E to be of a known Enum type), you can now remove E e from the lambda signature, as it is not really used anywhere. \$\endgroup\$ Commented Dec 2, 2016 at 20:51
  • \$\begingroup\$ @zeppelin No, I can't, because the challenge states "Your method will take two parameters, one a string and one an enum". \$\endgroup\$ Commented Dec 2, 2016 at 20:52
  • 1
    \$\begingroup\$ @Mego E should be Enum, as this is supposed to take in any enum, not just E \$\endgroup\$ Commented Dec 2, 2016 at 20:58
  • 1
    \$\begingroup\$ Yep, because it implies that E is <E extends Enum> instead (through OP failed to formulate that correctly), but in this case your E.values() code won't compile or work. \$\endgroup\$ Commented Dec 2, 2016 at 20:58
0
\$\begingroup\$

Java 4+, 96 bytes

boolean c(String a,Enum e){for(E f:values())if(f.toString().equals(a))return true;return false;}

Test code:

public enum E {
    A,B,C,D,E,F;
    public static void main(String[]a) {
        System.out.println(E.c("a",E));
        System.out.println(E.c("B",E));
        System.out.println(E.c("d",E));
        System.out.println(E.c("C",E));
        System.out.println(E.c("D",E));
        System.out.println(E.c("e",E));
        System.out.println(E.c("F",E));
    }
    boolean c(String a,Enum e){for(E f:values())if(f.toString().equals(a))return true;return false;}
}

Output:

false
true
false
true
true
false
true
\$\endgroup\$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.