【问题标题】:Object Comparison in JAVA...Part 2JAVA 中的对象比较...第 2 部分
【发布时间】:2012-02-14 17:18:45
【问题描述】:

这篇文章是我之前在此处找到的帖子的延续

Object comparison for equality : JAVA

根据收到的建议,我创建了以下类并使用 Eclipse IDE 执行了 equals()、hashcode() 覆盖 ....everything。但是,当我使用存储这些对象的数组列表的 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");
}

我得到的结果是“失败”

【问题讨论】:

    标签: java object comparison equals hashcode


    【解决方案1】:

    我在 Netbeans 中测试了您的代码,我得到了成功。有一个错字缺少“)” if(a1.contains(clsA2)

    “当然失败了。你的 id 字符串在你的测试代码中为空,如果发生这种情况,equals 方法被编写为返回 false。如果 firstId 和 secondId 都为空,或者如果其中一个为空,你应该允许相等和另一场比赛。” 真的不对。

    如果两个 id 都为 null,equals 将返回 true。仅当没有一个 ID 时。

    【讨论】:

    • 对不起,,,我已经更新了我的代码。两个对象属性具有相同的值..但我失败了....
    • 我现在唯一看到的是缺少的“)”,我希望你的代码中正确吗? if(a1.contains(clsA2)
    • 对不起,伙计们......这是我的错。我从一个我不应该使用的表中获取数据。这给出了我没有预料到的结果,因此提出了这个问题。上面的代码工作得很好。感谢大家的帮助。
    【解决方案2】:

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

    【讨论】:

    • 好吧,我的想法是比较是否存在 clsA2 的对象,该对象等效于 clsA1 中包含 clsA1 类型对象的数组中的某些内容。
    • 啊,好吧。似乎那时应该有效。我会尝试使用调试器逐步执行 equals 方法,以查看哪个检查失败。
    【解决方案3】:

    我只是复制粘贴并测试了您的代码。我得到了成功。 a1.contains(clsA2) 后面只漏了一个括号。 eclipse 生成的equals() 可以正确处理null

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      相关资源
      最近更新 更多