史上最全的针对的Java8map集合的新特性

153 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第13天,点击查看活动详情

前言

Java8的新特性出来之后,在Java中我们常用的集合Map也随之伴随了一些新的方法的出现,但是在日常的编码过程中是否都用上了Map集合的新方法。这些方法中还有很多利用了Lambda表达的写法,可以大大的简化我们的代码量。

getOrDefault

该方法的具体含义:返回指定键映射到的值,如果该映射不包含键的映射,则返回defaultValue。

    @Test
    public void test1(){
        HashMap<String, String> map = new HashMap<>();
        String orDefault = map.getOrDefault("key1", "value1");
        System.out.println(orDefault);  // value1
        map.put("key1","value1");
        String orDefault2 = map.getOrDefault("key1", "value2");
        System.out.println(orDefault2);  // value1
        System.out.println(map);        // {}
    }

forEach和putIfAbsent

putIfAbsent:如果指定的键还没有与值关联(或映射为空),则将其与给定值关联并返回空,否则返回当前值。就是key不存在的时候才添加返回空,key存在的时候不添加返回key关联的value

forEach方法map集合的遍历。

    @Test
    public void test2(){
        HashMap<String, String> map = new HashMap<>();
        String put = map.put("key1", "value1");
        System.out.println(put); // null
        String put3 = map.putIfAbsent("key1", "value3");
        System.out.println(put3); // value1
        String put4 = map.putIfAbsent("key3", "value3");
        System.out.println(put4); // null
        map.forEach((key,value)->{
            System.out.println(key);
            System.out.println(value);
        });
    }

remove

remove(var1,var2)当key和value都满足时候才移除集合中该元素。只有当指定键的项当前映射到指定值时,才移除该项。

    @Test
    public void test3(){
        HashMap<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.putIfAbsent("key2", "value2");
        map.putIfAbsent("key3", "value3");
        String key1 = map.remove("key1");
        System.out.println(key1); // value1
        boolean remove = map.remove("key2", "value3");
        System.out.println(remove);  // false
        System.out.println(map);  // {key2=value2, key3=value3}
    }

replace和replaceAll

replace(var1,var2,var3):仅当当前映射到指定值时,才替换指定键的条目,当key,和它原来的值都相同采用新值替换旧值。

replaceAll(BiFunction function):将每个条目的值替换为调用该条目上的给定函数的结果,直到处理完所有条目或函数抛出异常为止。由函数抛出的异常被转发给调用者。对该map集合的每一对元素按照返回的value进行替换。

    @Test
    public void test4(){
        HashMap<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.putIfAbsent("key2", "value2");
        map.putIfAbsent("key3", "value3");
        map.replace("key1","replace1");
        map.replace("key2","v2","replace2");
        System.out.println(map);
        map.replaceAll((key,value)->{
            return "abcd";
        });
        System.out.println(map);
    }

merge

merge():如果指定的键尚未与值关联或与空值关联,则将其与给定的非空值关联。否则,将用给定重映射函数的结果替换相关值,如果结果为空则删除。当组合一个键的多个映射值时,此方法可能有用。例如,创建或添加String msg到值映射:(常常用于往一个map集合里面添加元素,并且同时可以对添加的元素进行操作)

    @Test
    public void test5(){
        HashMap<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.putIfAbsent("key2", "value2");
        map.putIfAbsent("key3", "value3");
        map.put("abc","fafa");
        // 当map集合里面有abc时,才会进入Lambda表达式的方法  value1为key绑定的value,value2为新传入的value。
        // map集合里面没有abc时候,会添加key value进入map集合
        map.merge("abc","value4",(value1,value2)->{
            System.out.println(value1);  // fafa
            System.out.println(value2); // value4
            return value2;
        });
        System.out.println(map); //{key1=value1, null=value4, key2=value2, key3=value3}
    }

computeIfAbsent

computeIfAbsent:当不存在这个key1的时候,走后面的mappingFunction,存在改key,则返回改key对应的value。

    @Test
    public void test6(){
        HashMap<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.computeIfAbsent("key1",(v)->{
            System.out.println(v);  //不走
            return v;
        });
        System.out.println(map); // {key1=value1}
        map.computeIfAbsent("key2",(v)->{
            System.out.println(v);  // key2
            return v;
        });
        System.out.println(map); //{key1=value1, key2=key2}
    }

在遇到map嵌套集合的复杂的数据类型的时候可以巧妙的利用这个方法进行数据的添加。可以利用改方法巧妙的为一键多值的map添加元素。

    @Test
    public void test7(){
        HashMap<String, List<String>> map = new HashMap<>();
        // 如果缺席则计算,v就是前面的key
        // 存在的话返回原来的value,就是绑定的value
        List<String> list = map.computeIfAbsent("key1", (v) -> {
            System.out.println(v);  //key1
            List<String> arrayList = new ArrayList<>();
            arrayList.add(v);
            return arrayList;
        });
        list.add("value1");
        list.add("value2");
        list.add("value3");
        System.out.println(map); //{key1=value1, key2=key2}
    }

computeIfPresent

computeIfPresent:存在当前key的时候才会走后面的remappingFunction函数,BiFunction的两个参数前一个为key,后一个为value,返回他们的计算结果。不存在key的时候直接返回一个null。

    @Test
    public void test8(){
        HashMap<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        String v = map.computeIfPresent("key1", (v1, v2) -> {
            System.out.println(v1); // key1
            System.out.println(v2);  //value1
            return v1;
        });
        System.out.println(v);  //key1
        System.out.println(map); // {key1=value1}
        String v0 = map.computeIfPresent("key2", (v1, v2) -> {
            System.out.println(v1); // 不走
            System.out.println(v2);
            return v1;
        });
        System.out.println(v0); // null
        System.out.println(map); //{key1=key1}
    }

compute

给出一个key值和一个函数,然后这个函数根据key对应的键值对[key,value]计算出一个新的value,就叫newValue如果这个newValue的值是null,则从原来的map中移除key,compute返回null,如果这个newValue的值不为null,则更新key对应的值为newValue,compute返回newValue

    @Test
    public void test9(){
        HashMap<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        String v = map.compute("key1", (v1, v2) -> {
            System.out.println(v1); // key1
            System.out.println(v2);  //value1
            return v1;
        });
        System.out.println(v);  //key1
        System.out.println(map); // {key1=value1}
        String v0 = map.compute("key2", (v1, v2) -> {
            System.out.println(v1); // key2
            System.out.println(v2); // null
            return v1;
        });
        System.out.println(v0); // key2
        System.out.println(map); //{key1=key1}
    }

它是computeIfAbsentcomputeIfPresent的结合体,无论前一个参数给的Key是否存在,都会执行后面的Lambda表达式里面方法。当Lambda的返回结果为null时候就会从map集合中移除key,非空时候就会更新该值到Map集合。