美文网首页
map.computeIfAbsent

map.computeIfAbsent

作者: felixfeijs | 来源:发表于2021-04-14 09:23 被阅读0次
    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("1",1);
        map.put("2",2);
        map.put("3",3);
        Integer integer = map.computeIfAbsent("3", key -> new Integer(4));//key存在返回value
        Integer integer1 = map.computeIfAbsent("4", key -> new Integer(4));//key不存在执行函数存入
        System.out.println(integer);
        System.out.println(integer1);
        System.out.println(map.toString());
    }

相关文章

网友评论

      本文标题:map.computeIfAbsent

      本文链接:https://www.haomeiwen.com/subject/xteclltx.html