• A Guide to Java HashMap


    原文链接: A Guide to Java HashMap → https://www.baeldung.com/java-hashmap

    在这里插入图片描述

    常见方法

    HashMap<String, Integer> map = new HashMap<>();
    // {}
    
    map.put("Apple", 1);
    map.put("Banana", 2);
    map.put("Cherry", 3);
    // {Apple=1, Cherry=3, Banana=2}
    
    map.put("Apple", 1);
    map.put("Banana", 2);
    map.put("Banana", 3);
    // {Apple=1, Banana=3}
    
    map.remove("Banana");
    // {Apple=1, Cherry=3}
    
    boolean hasOrange = map.containsKey("Orange");
    int mapSize = map.size();
    
    map.clear();
    // {}
    
    Set<String> keys = map.keySet();
    Collection<Integer> values = map.values();
    Set<Map.Entry<String, Integer>> entries = map.entrySet();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    put

    [Q&A] 平时都使用Map的put方法,但是put方法返回值是啥?
    key对应存储的上个值

    Map<Object, Boolean> map = new HashMap<>();
    Boolean value1 = map.put("key", true);
    System.out.println(value1); // null key上个值为null,故返回null
    
    Boolean value2 = map.put("key", false);
    System.out.println(value2); // null key上个值为true,故返回true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    get

    # 原生方法
    Map<String, Integer> map = new HashMap<>();
    
    Integer age6 = map.get("name");              // Integer时返回null可以
    int age6 = map.get("name");                  // int时返回null报错
    Integer age5 = map.getOrDefault("name", 0);  // 取不到使用默认值
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    # MapUtils
    Map<String, Integer> map = new HashMap<>();
    
    Integer age1 = MapUtils.getInteger(map, "name");     // Integer时返回null可以
    int age3 = MapUtils.getInteger(map, "name");         // int时返回null报错
    Integer age1 = MapUtils.getInteger(map, "name", 0);   // 取不到返回null
    int age3 = MapUtils.getInteger(map, "name", 0);       // 取不到返回null
    
    dependency>
       <groupId>commons-collections</groupId>
       <artifactId>commons-collections</artifactId>
       <version>3.2.1</version>
       <scope>compile</scope>
    /dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    computeIfPresent

    Map<String, Integer> scores = new HashMap<>();
    scores.put("Alice", 85);
    scores.put("Bob", 95);
    System.out.println(scores);
    
    scores.computeIfPresent("Alice", (key, oldValue) -> {
        if (oldValue >= 85) {
            return oldValue + 5;
        }
        return oldValue;
    });
    System.out.println(scores);
    
    // {Bob=95, Alice=85}
    // {Bob=95, Alice=90}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    putIfAbsent

    Map<Object, Boolean> map = new HashMap<>();
    System.out.println(map);    // {}
    
    # 1、map不存在key,故返回null,并把当前key,value存储到Map中
    Boolean value1 = map.putIfAbsent("key", true);
    System.out.println(value1); // map不存在key,故返回null
    System.out.println(map);    // {key1=true}
    
    # 2、map存在key,故返回对应value,并忽略当前key,value
    Boolean value2 = map.putIfAbsent("key", false);
    System.out.println(value2); // map存在key,故返回存的true
    System.out.println(map);    // {key1=true}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    [Q&A] 存在的问题:
    当key对应的值为null时,调用putIfAbsent返回null无法判断是存在键(key,null),还是key不存在


  • 相关阅读:
    LLMs: 强化学习从人类反馈中学习Reinforcement learning from human feedback (RLHF)
    华为云API人脸识别服务FRS的感知力—偷偷藏不住的你
    基于JAVA+SpringBoot的新闻发布平台
    【计算机网络】P1 计算机网络概述
    矩阵分解方法(主要是非负矩阵分解)--学习笔记
    SQL学习之增删改查
    【Python】去除列表中的重复元素
    基于Qt的旅行最优时间费用模拟系统
    Protocol Buffers语法
    微信小程序开发(九):使用扩展组件库
  • 原文地址:https://blog.csdn.net/weixin_37646636/article/details/132758225