ArrayList的contains()方法始终使用自定义对象返回false [英] ArrayList's contains() method always returns false with custom object

查看:194
本文介绍了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方法中总会有一个String.还有一些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-这样就永远不会出现我在这里"消息.我一直在尝试一些不同的方法来处理我的代码,但这是因为这样做的想法是从String文本中获取单个字符,并检查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.

推荐答案

您的

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

equals方法对非null对象引用实现等效关系:

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

  • 它是自反的:对于任何非<​​c2>参考值xx.equals(x)应该返回true.
  • 它是对称的:对于任何非<​​c2>参考值xy,当且仅当y.equals(x)返回true时,x.equals(y)应该返回true.
  • 它是 transitive :对于任何非<​​c2>参考值xyz,如果x.equals(y)返回true并且y.equals(z)返回true,那么x.equals(z)应该返回true.
  • 这是一致的:对于任何非<​​c2>参考值xy,多次调用x.equals(y)会始终返回true或一致地返回false(提供)在对象的equals比较中使用的信息均未修改.
  • 对于任何非<​​c2>参考值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.

无法将Signequals的实例创建为字符串.

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

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

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