开发者社区 问答 正文

Hashmap cotainskey 结果有问题

public class HashMapDemo {
public static void main(String args[]) {
// create hash map
HashMap> TT = new HashMap<>();
ArrayList gIds = null;
Test t1=new Test(0, 1, 0, 0, 0);
Test t2=new Test(0, 1, 0, 0, 0);
TT.put(t1, gIds);
   if (!TT.containsKey(t2)) {
        System.out.println("不含");
    } else {
        gIds = TT.get(t2);
        System.out.println("找到");
    }
}
}
class Test{
public int a;
public int b;
public int c;
public int d;
public int e;
public Test(int a,int b,int c,int d,int e){
this.a=a;
this.b=b;
this.c=c;
this.d=d;
this.e=e;
}
}

输出的结果是不含,请问为什么,我该怎么改呢?

展开
收起
蛮大人123 2016-03-12 19:13:48 2925 分享 版权
2 条回答
写回答
取消 提交回答
  • 看下是否存在并发操作的情况,现看下key的类型,如果key类型是对象,看下是否有重写过equals方法和hashcode方法
    2019-07-17 19:01:30
    赞同 展开评论
  • 我说我不帅他们就打我,还说我虚伪

    需要重写equals,否则比较的是引用,不是你的属性。

    2019-07-17 19:01:30
    赞同 展开评论
问答地址: