qq_41084438
qq_41084438
2019-04-18 16:05

关于JAVA Set容器contains的问题

  • java

class AAA{
int x;
public AAA(int x) { this.x=x;}
public int hashCode() { return x*11+17;}
public boolean equals(AAA e) { return this.x==e.x;}
}

public class 参数{
public static void main(String[] args) {
HashSet set = new HashSet();
AAA a1 = new AAA(5);
AAA a2 = new AAA(5);
System.out.println(a1.equals(a2));
System.out.println(set.add(a1));
System.out.println(set.contains(a2));
}
}

明明a1和a2是相等的,但contains返回的是false,请问要怎么改?
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答

为你推荐