How to check equals on enum value?

How can I check equality on a value of an enum? I tried the following, but it fails. WHy?

class enum Test {
    LETTER("1A");

    private String value;
    public Test(String value) {
        this.value = value;
    }

    @Override
    public void toString() {
        return value;
    }
}


String teststring = "1A".
Sysout(teststring.equals(Test.LETTER)); //false. Why???

The comparison is returning false because you're comparing a String instance with an Enum instance.

@RohitJain already gave a great explanation about why the toString() method doesn't get called in your comparison in his answer, and how you should use it if it's what you want to do.

However, toString() shouldn't be used for comparison purposes, it should be overriden only to provide a custom String representation of the object.

You could compare the property you're really intereseted in comparing if you do this:

String teststring = "1A".
Sysout(teststring.equals(Test.LETTER.getValue())); // Will return true

Of course you should provide a getter for the value attribute or let it be visible to the operation doing the comparison.

You could also code a isEqual() method in your enum like this:

enum Test {
    LETTER("1A");

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

    @Override
    public String toString() {
        return value;
    }

    public boolean isEqual(String value) { 
        return this.value.equals(value);
    }
}

And just do Test.LETTER.isEqual(teststring) to do this comparison.

Or even a static getByValue(String value) method to get the enum instance for a given value :

enum Test { 
    ...
    public static Test getByValue(String val) { 
        for (Test c : values()) {
            if (c.value.equals(val)) {
                return c;
            }
        }
    } 
} 

Test x = Test.getByValue("1A"); // x == Test.LETTER

There are so much of other compiler errors in your code:

  • class enum Test should be enum Test .
  • An enum's constructor can only be private .
  • Return type of toString() method is void . That is not a valid override. Make the return type String .
  • I don't know whether these errors also persist in your original code, or it's just a mistake in your post.

    Now coming to the concrete question. When you print an enum constant, the toString() method is automatically called on that. So, you would get 1A when you print Test.LETTER .

    This is not true when passing Test.LETTER as argument to any method. You would have to call toString() explicitly. So, that should be:

    System.out.println(teststring.equals(Test.LETTER.toString()));
    

    Another way is to provide a method in your enum itself, which gets you an enum constant for a particular String value:

    public static Test of(String str) {
        for (Test test: values()) {
            if (test.value.equals(str)) {
                return test;
            }
        }
        return null;
    }
    

    You can even cache the array returned by values() method, so that it not created again and again.

    And then just compare the enum constant as:

    String teststring = "1A";
    System.out.println(Test.of(teststring) == Test.LETTER);
    

    Just do the check the following way and it will work

    teststring.equals(Test.LETTER.toString())
    

    Test.LETTER is an enum so it will never be equal to a String, but its value can be equal to a String so you have to check directly using the value of the enum.

    链接地址: http://www.djcxy.com/p/91842.html

    上一篇: 重构if语句以使用适当的模式

    下一篇: 如何检查枚举值等于?