Java 如何检查枚举值的相等?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19586234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check equals on enum value?
提问by membersound
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???
采纳答案by Xavi López
The comparison is returning false
because you're comparing a String
instance with an Enum
instance.
比较返回false
是因为您将String
实例与Enum
实例进行比较。
@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.
@RohitJain 已经很好地解释了为什么toString()
在他的回答中没有在您的比较中调用该方法,以及如果这是您想要做的,您应该如何使用它。
However, toString()
shouldn't be used for comparison purposes, it should be overriden only to provide a custom String representation of the object.
但是,toString()
不应将其用于比较目的,它应仅被覆盖以提供对象的自定义字符串表示形式。
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.
当然,您应该为该value
属性提供一个 getter,或者让它对进行比较的操作可见。
You could also code a isEqual()
method in your enum
like this:
你也可以像这样编写一个isEqual()
方法enum
:
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.
而只是做Test.LETTER.isEqual(teststring)
这个比较。
Or even a static getByValue(String value)
method to get the enum instance for a given value
:
甚至是static getByValue(String value)
获取给定枚举实例的方法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
回答by user1983983
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.
Test.LETTER
是一个枚举,所以它永远不会等于一个字符串,但它的值可以等于一个字符串,所以你必须直接使用枚举的值进行检查。
回答by Silviu Burcea
Because String's equals
method is checking for the object received to be String
as well. It should work if you try with Test.LETTER.toString()
.
因为String's equals
方法String
也在检查接收到的对象。如果您尝试使用Test.LETTER.toString()
.
回答by Rohit Jain
There are so much of other compiler errors in your code:
您的代码中还有很多其他编译器错误:
class enum Test
should beenum Test
.- An enum's constructor can only be
private
. - Return type of
toString()
method isvoid
. That is not a valid override. Make the return typeString
.
class enum Test
应该是enum Test
。- 枚举的构造函数只能是
private
. toString()
方法的返回类型是void
。这不是有效的覆盖。使返回类型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
.
现在进入具体问题。当您打印一个enum
常量时,toString()
会自动调用该方法。所以,1A
当你打印时你会得到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:
当Test.LETTER
作为参数传递给任何方法时,情况并非如此。您必须toString()
明确调用。所以,应该是:
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:
另一种方法是在您的枚举本身中提供一个方法,它为您enum
提供一个特定字符串值的常量:
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.
你甚至可以缓存values()
方法返回的数组,这样它就不会一次又一次地创建。
And then just compare the enum constant as:
然后将枚举常量比较为:
String teststring = "1A";
System.out.println(Test.of(teststring) == Test.LETTER);
回答by SSP
This will return false flag because of comparison between String object and ENUM instance.
由于 String 对象和 ENUM 实例之间的比较,这将返回 false 标志。
If you do this:
如果你这样做:
String variable1 = "1A".
boolean flag = variable1.equals(Test.LETTER.value);
system.out.println(flag);
This will return
这将返回
True
all though
尽管如此
IN java .equalsmethods look like this
IN java .equals方法看起来像这样
public final boolean equals(Object other) {
return this==other;
}
I always use == because it is null safe.
我总是使用 == 因为它是空安全的。