JAVA 中的对象比较...第 2 部分

发布于 2025-01-05 22:16:34 字数 2126 浏览 1 评论 0原文

这篇文章是我上一篇文章的延续,

对象比较平等:JAVA

基于根据我收到的建议,我创建了以下类并覆盖了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑾兮 2025-01-12 22:16:34

我在 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.

有深☉意 2025-01-12 22:16:34

您收到“失败”消息是因为您在未将 clsA2 添加到 a1 时检查 a1 是否包含 clsA2。检查 a2.contains(clsA2) 是否应打印“Success”

You're getting a "Failure" because you're checking if a1 contains clsA2 when you haven't added clsA2 to a1. Checking if a2.contains(clsA2) should print "Success"

话少情深 2025-01-12 22:16:34

我只是复制粘贴并测试了您的代码。我成功了。您只在 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-generated equals() does handle null properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文