栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA----双列集合---Map用法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA----双列集合---Map用法

1.Map集合的概述

        Map集合是一种双列集合,每个元素包含两个数据。
        Map集合的每个元素的格式:key=value(键值对元素)。
        Map集合也被称为“键值对集合”。

2.Map集合的完整格式:{key1=value1 , key2=value2 , key3=value3 , …} 3.Map集合体系特点:

        Map集合的特点都是由键决定的。
        Map集合的键是无序,不重复的,无索引的,值不做要求(可以重复)。
        Map集合后面重复的键对应的值会覆盖前面重复键的值。
        Map集合的键值对都可以为null。

4.Map集合体系:

 一:添加元素

1.如果键是第一次存储,就直接存储元素,返回null

2.如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值

   @Test
    public void test0(){
        Mapmap =new HashMap<>();
        map.put("陈浩楠", 25);
        map.put("浩楠", 90);
        System.out.println("陈浩楠对应的值 >>>"+map.get("陈浩楠"));
        System.out.println("浩楠的值 >>>"+map.get("浩楠"));
        map.put("陈浩楠",20);
        System.out.println("陈浩楠对应的值 >>>"+map.get("陈浩楠"));
        System.out.println("浩楠的值 >>>"+map.get("浩楠"));
    }

        结果是:

 二:删除元素功能

1.移除所有的键值对元素

2.根据键删除键值对元素,并把值返回

   @Test
    public void test02(){
        Mapmap =new HashMap<>();
        map.put("陈浩楠", 25);
        map.put("浩楠", 90);
        System.out.println("陈浩楠对应的值 >>>"+map.get("陈浩楠"));
        System.out.println("浩楠的值 >>>"+map.get("浩楠"));
        map.remove("陈浩楠");
        System.out.println("陈浩楠对应的值 >>>"+map.get("陈浩楠"));
        System.out.println("浩楠的值 >>>"+map.get("浩楠"));
    }

        结果是:

三: 判断元素

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

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

3.判断集合是否为空

    @Test
    public void test03(){
            Map map = new HashMap<>();
            for (int i = 0; i < 10; i++) {
                map.put("沈腾" + (i + 1), ThreadLocalRandom.current().nextInt(100));
            }
            System.out.println(map);
            boolean key = map.containsKey("沈腾56");
            boolean value = map.containsValue(10);
            boolean empty = map.isEmpty();
            System.out.println(key);
            System.out.println(value);
            System.out.println(empty);
        }

        结果是:

 四:获取元素

  • V get(Object key):根据键获取值
  • Set keySet():获取集合中所有键的集合
 @Test
    public void test04() {
        Map map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            map.put("沈腾" + (i + 1), ThreadLocalRandom.current().nextInt(1000));
        }
        Set strings = map.keySet();
        Iterator it = strings.iterator();
        while (it.hasNext()) {
            String key = it.next();
            System.out.println("key >" + key + "tvalue >" + map.get(key));
        }

        结果是:

         

五:设置默认值 

  @Test
    public void test05(){
        Map map = new HashMap<>();
        map.put("陈浩楠", 95);
        map.put("浩楠", 90);
        System.out.println("size >>> " + map.size());
        System.out.println("陈浩楠 >>> " + map.get("陈浩楠"));
        System.out.println("浩楠 >>> " + map.getOrDefault("浩楠", 0));
        map.remove("浩楠");
        System.out.println("size >>> " + map.size());
        System.out.println("陈浩楠 >>> " + map.get("陈浩楠"));
        System.out.println("浩楠 >>> " + map.getOrDefault("浩楠", 0));
    }

        结果是:

六: HashMap根据key遍历

   @Test
    public void test06(){
        Map map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            map.put("陈浩楠" + (i + 1), ThreadLocalRandom.current().nextInt(100));
        }
        // 获取集合中所有的key
        Set keySet = map.keySet();
        Iterator it = keySet.iterator();
        while (it.hasNext()) {
            String key = it.next();
            System.out.println("key >>> " + key + "tvalue >>> " + map.get(key));
        }
    }

输出:

七: HashMap遍历value值

   @Test
    public void test07(){
        Map map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            map.put("key" + i , "value" + i);
        }
        Collection values = map.values();
        Iterator it = values.iterator();
        while (it.hasNext()){
            String value = it.next();
            System.out.println(value);
        }
    }

输出:

 八:使用entrySet遍历 HashMap

  @Test
    public void test08(){
        Map map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            map.put("陈浩楠" + (i + 1), ThreadLocalRandom.current().nextInt(100));
        }
        Set> entrySet = map.entrySet();
        Iterator> it = entrySet.iterator();
        while (it.hasNext()){
            Map.Entry entry = it.next();
            System.out.println("key >>> " + entry.getKey() + "tvalue >>> " + entry.getValue());
        }
    }

输出:

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/845202.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号