Checking String for enum in Java

-4

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!

SamCle88

Posted 2016-12-01T09:30:40.300

Reputation: 23

Question was closed 2017-01-11T19:29:32.707

What should the method do? With the current spec, always returning true is allowed. – Zgarb – 2016-12-01T09:43:27.343

I'll edit, probably the request on parameters is too restrictive, I'll also add an example. – SamCle88 – 2016-12-01T09:45:35.383

3Single-language challenges are also discouraged here. Unless the challenge only makes sense for Java, you should allow all languages. – Zgarb – 2016-12-01T09:47:47.717

In this case, I'm starting from a Java enum so, unless another language can deal with a Java class, I don't see why I should allow other languages. (Sorry for my obstinacy, but I'm new here and I wanted this to solve a more practical than theoretical problem). – SamCle88 – 2016-12-01T09:54:49.593

Okay, now I get what the challenge is about. Because it's so focused, I think it would be received better as a [tag:tips] question that asks for golfing advice, like "I want to check whether the name of a Java enum value matches a given string, is there a shorter way than this?" – Zgarb – 2016-12-01T10:09:10.967

Ok, tried to edit in that sense. May I know if there are some more not clear points left? – SamCle88 – 2016-12-01T10:54:14.600

So is the code to check whether a given string is found in an enum? – george – 2016-12-01T11:07:45.590

1The 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; [tag:tips] questions are not competitions. – Zgarb – 2016-12-01T11:37:01.740

Edited as suggested. Thank you. – SamCle88 – 2016-12-01T13:57:28.140

Can we assume that the enum class will be E? – Mego – 2016-12-02T10:55:49.690

I prefer if you don't make assumptions on the class E because I'm going to use it within a context with generics. – SamCle88 – 2016-12-02T11:11:20.187

ockquote>

I prefer if you don't make assumptions on the class E

That is a bit self-contradictory, as looking at your reference method you do use a literal E type at least twice there. You should then declare it as: <E extends Enum> void check(String s,E e) instead. – zeppelin – 2016-12-02T19:53:04.790

1Otherwise 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). – zeppelin – 2016-12-02T19:54:53.217

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
  • < – Peter Taylor – 2017-01-11T17:39:47.410

    I'll try to edit it. – SamCle88 – 2017-01-11T21:07:33.090

    There are now two reference implementations with different signatures. In my opinion that's adding to the confusion, not reducing it. – Peter Taylor – 2017-01-12T09:05:50.467

    I don't get where the problem is. This method takes a string and a variable that must be an instance of some enum and returns true iff the string represents an instance of the same enum as the example given by the second argument. – SamCle88 – 2017-01-12T09:57:45.963

    Answers

    1

    Java 8, 86 bytes

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

    Try it online!

    Mego

    Posted 2016-12-01T09:30:40.300

    Reputation: 32 998

    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. – Pavel – 2016-12-02T20:50:55.823

    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. – zeppelin – 2016-12-02T20:51:56.787

    @zeppelin No, I can't, because the challenge states "Your method will take two parameters, one a string and one an enum". – Mego – 2016-12-02T20:52:38.680

    1@Mego E should be Enum, as this is supposed to take in any enum, not just E – Magic Octopus Urn – 2016-12-02T20:58:34.270

    1Yep, 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. – zeppelin – 2016-12-02T20:58:43.593

    The test cases imply that the function should take a value, not an Enum class. – Mego – 2016-12-02T21:05:16.187

    0

    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
    

    Magic Octopus Urn

    Posted 2016-12-01T09:30:40.300

    Reputation: 19 422