zl程序教程

您现在的位置是:首页 >  Java

当前栏目

Java中Map集合常见使用

2023-02-18 16:35:57 时间

提要

Map集合概述

  • Map<K,V>
  • K:键的类型
  • V:值的类型
  • 把建映射到值的对象中,每一个建最多映射到一个值
  • 不能有重复的键

创建

  • 具体的实现类是:HashMap
  • 用多态的方式创建
//创建Map集合
        Map<String, String> hashMap = new HashMap<String, String>();

添加元素

  • put()
  • 切记 K 重复会替换之前的值
//创建Map集合
        Map<String, String> hashMap = new HashMap<String, String>();
        //添加
        hashMap.put("姓名","老八");
        hashMap.put("姓名","老九");
        hashMap.put("666","~~~");
        hashMap.put("gg","giao");
        //输出
        System.out.println(hashMap);

输出结果如下

{gg=giao, 姓名=老九, 666=~~~}

删除元素

1).根据键删除指定键值对元素

  • remove(Object key)
  • 返回删除K对应的V
  • 如果没有删除的 K 那返回 null
//创建HashMap
        Map<String, String> map =new HashMap<String, String>();
        //添加元素
        map.put("科一","95");
        map.put("科二","90");
        map.put("科三","80");
        map.put("科四","90");
        System.out.println(map);//输出
        //删除元素
        map.remove("科四");
        System.out.println(map);//输出

结果:

{科一=95, 科二=90, 科四=90, 科三=80}
{科一=95, 科二=90, 科三=80}

2).移除所有键值对元素

  • clear()
        //创建HashMap
        Map<String,String> map = new HashMap<String, String>();
        //添加元素
        map.put("壹","一");
        map.put("贰","二");
        map.put("弎","三");
        System.out.println(map);//输出
        //删除所有
        map.clear();
        System.out.println(map);//输出,跑路

结果:

{贰=二, 壹=一, 弎=三}
{}

获取集合长度

  • 也就是集合中键值对的个数
  • size()
  • 返回 int 类型
        //创建Map集合
        Map<String, String> hashMap = new HashMap<String, String>();
        //添加
        hashMap.put("姓名","老八");
        hashMap.put("666","~~~");
        hashMap.put("gg","giao");
        //获取长度
        int size = hashMap.size();
        //输出
        System.out.println(hashMap);
        System.out.println("长度:"+size);

结果:

{gg=giao, 姓名=老九, 666=~~~}
长度:3

Map集合获取方法

根据键获取值

  • get()
  • 如果没有 则为 null
        //创建Map集合
        Map<String, String> map = new HashMap<String, String>();
        //添加元素
        map.put("壹","一");
        map.put("贰","二");
        map.put("弎","三");
        //根据键获取值
        String s = map.get("壹");
        System.out.println(s);//输出

获取所有键的集合

  • SetkeySet()
        //创建Map集合
        Map<String, String> map = new HashMap<String, String>();
        //添加元素
        map.put("壹","一");
        map.put("贰","二");
        map.put("弎","三");
         //获取所有键的集合
        Set<String> stringSet = map.keySet();
        //循环遍历
        for (String s : stringSet) {
            System.out.println(s);//打印
        }

获取所有值的集合

  • values();
        //创建Map集合
        Map<String, String> map = new HashMap<String, String>();
        //添加元素
        map.put("壹","一");
        map.put("贰","二");
        map.put("弎","三");

        //获取所有值
        Collection<String> values = map.values();
        //循环遍历
        for (String value : values) {
            System.out.println(value);//打印
        }

获取所有键值对对象集合

  • entrySet()
  • Set<Map.Entry<K,​V>> 存储是这个对象,用Map.Entry对象获取键值对
  • Map.Entry对象
    • getKey():得到键
    • getValue():得到值
        //创建Map集合
        Map<String, String> map = new HashMap<String, String>();
        //添加元素
        map.put("壹","一");
        map.put("贰","二");
        map.put("弎","三");

        //获取所有键值对对象集合
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        //遍历
        for (Map.Entry<String, String> stringEntry : entrySet) {
            String key = stringEntry.getKey();//获取键
            String value = stringEntry.getValue();//获取值
            System.out.println(key+"==="+value);//输出
        }

判断集合是否包含指定的键

  • containsKey()
  • 返回 boolean类型,true:有,false:没有
  • 注意是键 也就是K
        //创建Map集合
        Map<String, String> hashMap = new HashMap<String, String>();
        //添加元素
        map.put("壹","一");
        map.put("贰","二");
        map.put("弎","三");
        //判断集合中是否有指定的键
        boolean key = map.containsKey("壹");
        System.out.println(key);//输出

判断集合是否包含指定的值

  • containsValue()
  • 返回 boolean类型,true:有,false:没有
  • 注意是值 也就是V
        //创建Map集合
        Map<String, String> hashMap = new HashMap<String, String>();
        //添加元素
        map.put("壹","一");
        map.put("贰","二");
        map.put("弎","三");
        //判断集合中是否有指定的值
        boolean value = map.containsValue("一");
        System.out.println(value);//输出

判断集合是否为空

        //创建Map集合
        Map<String, String> hashMap = new HashMap<String, String>();
        //添加元素
        map.put("壹","一");
        map.put("贰","二");
        map.put("弎","三");
        //判断集合是否为空
        boolean mapEmpty = map.isEmpty();
        System.out.println(mapEmpty);//输出