前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java Map的containsKey(Object key)和containsValue(Object value)方法

Java Map的containsKey(Object key)和containsValue(Object value)方法

作者头像
JavaEdge
发布2018-05-16 12:06:48
2K0
发布2018-05-16 12:06:48
举报
文章被收录于专栏:JavaEdgeJavaEdge
public void testContainsKeyOrValue(){

        Scanner sc = new Scanner(System.in);
        //Key
        System.out.println("请输入要查询的学生id:");
        String id = sc.next();
        System.out.println("你输入的学生id为:"+id+",在学生映射表中是否存在"+
            students.containsKey(id));
        if(students.containsKey(id)){
            System.out.println("对应的学生为:"+students.get(id).name);
        }

        //Value
        System.out.println("请输入要查询的学生姓名:");
        String name = sc.next();
        if(students.containsValue(new Student(null,name))){
            System.out.println("在学生映射表中,确实包含学生:"+name);
        }
        else{
            System.out.println("在学生映射表中不存在这个学生");
        }
    }

运行结果:

请输入学生id: 
1 
输入学生姓名以创建学生: 
小明 
成功添加学生:小明 
请输入学生id: 
2 
输入学生姓名以创建学生: 
哈哈 
成功添加学生:哈哈 
请输入学生id: 
3 
输入学生姓名以创建学生: 
极客咯 
成功添加学生:极客咯 
总共有3个学生 
学生:小明 
学生:哈哈 
学生:极客咯 
请输入要查询的学生id: 
2 
你输入的学生id为:2,在学生映射表中是否存在true 
对应的学生为:哈哈 
请输入要查询的学生姓名: 
小明 
在学生映射表中不存在这个学生

结果分析: 可以看到,通过containsKey(Object key)方法比较的结果返回true,是我们想要的结果。通过containsValue(Object value)方法比较的结果返回是false,但是我们确实是有一个名字叫小明的学生啊。为什么呢?

查看containsKey(Object key)和containsValue(Object value)的API说明:

  • containsKey(Object key):Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)
  • containsValue(Object value):Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.

可以看到,都调用了equals()方法进行比较!因此可以回答为什么了,我们的Key是String类型的,String类型的equals()比较的是字符串本身的内容,所以我们根据键去查找学生的结果是true。而Value是Student类型的,equals()是直接用==实现的,==比较的是对象的引用地址,当然返回结果是false(参考equals()与==的区别与实际应用)。所以,要在Map中通过学生的名字判断是否包含该学生,需要重写equals()方法。 在Student.java中重写equals()方法:

@Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

再次运行,得到运行结果: 请输入学生id: 1 输入学生姓名以创建学生: 小明 成功添加学生:小明 请输入学生id: 2 输入学生姓名以创建学生: 哈哈 成功添加学生:哈哈 请输入学生id: 3 输入学生姓名以创建学生: 极客咯 成功添加学生:极客咯 总共有3个学生 学生:小明 学生:哈哈 学生:极客咯 请输入要查询的学生id: 2 你输入的学生id为:2,在学生映射表中是否存在true 对应的学生为:哈哈 请输入要查询的学生姓名: 小明

在学生映射表中,确实包含学生:小明

结果分析: 通过重写equals()实现了Map中通过学生姓名查找学生对象(containsValue()方法)。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档