ArrayList 的 contains() 方法对于自定义对象总是返回 false [英] ArrayList's contains() method always returns false with custom object

查看:33
本文介绍了ArrayList 的 contains() 方法对于自定义对象总是返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理我的代码时遇到了一些麻烦,我会给你一个简单的例子(虽然它会更复杂一些,但这个简单的代码也不能正常工作).

I'm having some troubles with getting on with my code, I'll give you an simple example (though it's going to be a little more complex, this simple code doesn't work properly either).

class Sign {

  private String char;
  private Integer freq;

  public Sign(String c) {
  this.char = c; 
  }

  @Override
  public boolean equals(Object o) {

   String check = (String)o;
   return check.equals(this.char);
  }

  @Override
  public int hashCode() {

    int hash = 7;
    hash = 31 * hash + this.char.hashCode();
    return hash;
}

}

为了简单起见,我假设在 equals 方法中总会有一个字符串.还有一些 hashCode() 也可以确保 contains() 方法可以工作,这是测试本身:

I assume that there's always will be a String in equals method for simplicity reasons. There's some hashCode() also to make sure that the contains() method will work and here's the test itself:

    ArrayList<Sign> queueOfSigns = new ArrayList<>();

    Sign test = new Sign("C");
    String c = "C";
    queueOfSigns.add(test);

    if(queueOfSigns.contains("C"))
        System.out.println("I am here!");

无论如何,在这种情况下,这个简单的测试代码总是返回 false - 所以我在这里"消息永远不会出现.我一直在尝试一些不同的方法来处理我的代码,但这是因为这样做的想法是从字符串文本中获取单个字符并检查单个字符是否已经存在于 ArrayList 中.尽管如此 - 如果没有让这个简单的测试正常工作,我无法继续前进,所以我想问你 - 我错过了什么.这是我第一次真正使用 equals() 和 hashCode() 方法让我自己的对象与 contains() 方法正常工作.

No matter what, this simple test-code always returns false in that case - so "I'm here" message never appears. I've been trying some different ways of approach my code but it was because the idea of this is to get single characters from String text and check whether the single character is already present in the ArrayList. Nevertheless - without getting this simple test working properly I can't move on, so I would like to ask you - what am I missing. It's my first time actually with using equals() and hashCode() methods to get my own object working properly with contains() method.

推荐答案

您的 equals 实现不正确.equals 有特定的契约;该代码试图违反该合同.来自文档:

Your equals implementation is incorrect. equals has a specific contract; that code attempts to violate that contract. From the documentation:

equals 方法在非空对象引用上实现等价关系:

The equals method implements an equivalence relation on non-null object references:

  • 它是自反:对于任何非<​​code>null引用值xx.equals(x)应该返回 true.
  • 它是对称:对于任何非<​​code>null引用值xyx.equals(y) 应该返回 true 当且仅当 y.equals(x) 返回 true.
  • 它是传递:对于任何非<​​code>null引用值xyz,如果 x.equals(y) 返回 true 并且 y.equals(z) 返回 true,那么 x.equals(z) 应该返回 true.
  • 一致:对于任何非<​​code>null引用值xy,多次调用x.equals(y) 始终返回 true 或始终返回 false,前提是在 equals 比较中没有使用信息对象被修改.
  • 对于任何非<​​code>null 引用值xx.equals(null) 应返回false.
  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

没有办法让你的 Sign 类的实例 equals 成为一个字符串.

There's no way to make an instance of your Sign class equals to a string.

这篇关于ArrayList 的 contains() 方法对于自定义对象总是返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆