JAVA 中的对象比较...第 2 部分
这篇文章是我上一篇文章的延续,
基于根据我收到的建议,我创建了以下类并覆盖了 equals()、hashcode() ....一切都使用 Eclipse IDE。但是,当我使用存储这些对象的数组列表的 contains() 方法来比较引用同一类的两个不同对象时,我仍然得到错误。我不知道我的实施有什么问题。希望帮助排除故障。
public class ClassA {
private String firstId;
private String secondId;
/**
* @return the firstId
*/
public String getFirstId() {
return firstId;
}
/**
* @param firstId the firstId to set
*/
public void setFirstId(String firstId) {
this.firstId = firstId;
}
/**
* @return the secondId
*/
public String getSecondId() {
return secondId;
}
/**
* @param secondId the secondId to set
*/
public void setSecondId(String secondId) {
this.secondId = secondId;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((firstId == null) ? 0 : firstId.hashCode());
result = PRIME * result + ((secondId == null) ? 0 : secondId.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ClassA other = (ClassA) obj;
if (firstId == null) {
if (other.firstId != null)
return false;
} else if (!firstId.equals(other.firstId))
return false;
if (secondId == null) {
if (other.secondId != null)
return false;
} else if (!secondId.equals(other.secondId))
return false;
return true;
}
}
ClassA clsA1 = new ClassA();
ClassA clsA2 = new ClassA();
clsA1.setFirstId("value1");
clsA1.setSecondId("value2");
clsA2.setFirstId("value1");
clsA2.setSecondId("value2");
ArrayList a1 = new ArrayList();
ArrayList a2 = new ArrayList();
a1.add(clsA1);
a2.add(clsA2);
if(a1.contains(clsA2)
{
System.out.println("Success");
}
else
{
System.out.println("Failure");
}
我得到的结果是“失败”
This post is continuation of my previous post found here
Object comparison for equality : JAVA
Based on the suggestions I received, I created the following class and did a equals(), hashcode() override ....everything using Eclipse IDE. However I still get a false when I compare two different objects which reference the same class using the contains() method of an arraylist in which these objects are stored. I dont know what is wrong in my implementation. Would like help troubleshooting.
public class ClassA {
private String firstId;
private String secondId;
/**
* @return the firstId
*/
public String getFirstId() {
return firstId;
}
/**
* @param firstId the firstId to set
*/
public void setFirstId(String firstId) {
this.firstId = firstId;
}
/**
* @return the secondId
*/
public String getSecondId() {
return secondId;
}
/**
* @param secondId the secondId to set
*/
public void setSecondId(String secondId) {
this.secondId = secondId;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((firstId == null) ? 0 : firstId.hashCode());
result = PRIME * result + ((secondId == null) ? 0 : secondId.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ClassA other = (ClassA) obj;
if (firstId == null) {
if (other.firstId != null)
return false;
} else if (!firstId.equals(other.firstId))
return false;
if (secondId == null) {
if (other.secondId != null)
return false;
} else if (!secondId.equals(other.secondId))
return false;
return true;
}
}
ClassA clsA1 = new ClassA();
ClassA clsA2 = new ClassA();
clsA1.setFirstId("value1");
clsA1.setSecondId("value2");
clsA2.setFirstId("value1");
clsA2.setSecondId("value2");
ArrayList a1 = new ArrayList();
ArrayList a2 = new ArrayList();
a1.add(clsA1);
a2.add(clsA2);
if(a1.contains(clsA2)
{
System.out.println("Success");
}
else
{
System.out.println("Failure");
}
I get the result as "Failure"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 Netbeans 中测试了您的代码,结果成功。有一个错字,缺少“)”
if(a1.contains(clsA2)
"当然失败了。你的测试代码中的 id 字符串为 null,如果发生这种情况,equals 方法被编写为返回 false。如果firstId 和 secondaryId 都为 null,也许你应该允许相等,或者如果其中一个为空并且另一个都匹配。”
实在是不太对劲。
如果两个 id 都为 null,则 equals 将返回 true。仅当一个 ID 不存在时。
I tested your code, I get Success, in Netbeans. There is a Typo a missing ")" in
if(a1.contains(clsA2)
"Of course it fails. Your id Strings are null in your test code, and the equals method is written to return false if this occurs. Perhaps you should allow equality if both firstId and secondId are null, or if either is null and both of the other match."
Isn't really right.
If both ids are null, the equals will return true. Only if one ID isn't.
您收到“失败”消息是因为您在未将
clsA2
添加到a1 时检查
。检查a1
是否包含clsA2
a2.contains(clsA2)
是否应打印“Success”You're getting a "Failure" because you're checking if
a1
containsclsA2
when you haven't addedclsA2
toa1
. Checking ifa2.contains(clsA2)
should print "Success"我只是复制粘贴并测试了您的代码。我成功了。您只在
a1.contains(clsA2)
之后漏掉了一个括号。 eclipse 生成的equals()
确实可以正确处理null
。I just copy-pasted and tested your code. I got Success. You missed only a parenthesis after
a1.contains(clsA2)
. The eclipse-generatedequals()
does handlenull
properly.