列表中的java contains()方法未按预期工作
contains()
方法的api说
“如果此列表包含指定的元素,则返回true。更正式地说,当且仅当此列表包含至少一个元素e时,才返回true,即(o==null?e==null:o.equals(e))。 “
我在类中重写了equals()
方法,但是contains()
在检查时仍然返回false
我的代码
class Animal implements Comparable<Animal>{
int legs;
Animal(int legs){this.legs=legs;}
public int compareTo(Animal otherAnimal){
return this.legs-otherAnimal.legs;
}
public String toString(){return this.getClass().getName();}
public boolean equals(Animal otherAnimal){
return (this.legs==otherAnimal.legs) &&
(this.getClass().getName().equals(otherAnimal.getClass().getName()));
}
public int hashCode(){
byte[] byteVal = this.getClass().getName().getBytes();
int sum=0;
for(int i=0, n=byteVal.length; i<n ; i++)
sum+=byteVal[i];
sum+=this.legs;
return sum;
}
}
class Spider extends Animal{
Spider(int legs){super(legs);}
}
class Dog extends Animal{
Dog(int legs){super(legs);}
}
class Man extends Animal{
Man(int legs){super(legs);}
}
请原谅课堂背后的错误概念,我只是在测试我对概念的理解
现在,当我尝试这个方法时,它会打印false
,即使覆盖了equals
List<Animal> li=new ArrayList<Animal>();
Animal a1=new Dog(4);
li.add(a1);
li.add(new Man(2));
li.add(new Spider(6));
List<Animal> li2=new ArrayList<Animal>();
Collections.addAll(li2,new Dog(4),new Man(2),new Spider(6));
System.out.println(li2.size());
System.out.println(li.contains(li2.get(0))); //should return true but returns false
# 1 楼答案
AsJLS-8.4.8.1 specify
签名必须与覆盖签名相同,在您的情况下,该签名将被忽略
# 2 楼答案
您重载了
equals
,而不是重写它。要重写Object
的equals
方法,必须使用相同的签名,这意味着参数必须是Object
类型改为: