HashMap常用方法

HashMap常用方法

代码

public class HashMapDemo {
    public Map<String,String> getInfo(){
        Map<String, String> map = new HashMap<>(8);
        map.put("1", "a");
        map.put("2", "b");
        map.put("3", "c");
        map.put("4", "d");
        return map;
    }

    /**
     * 如果不存在则赋值
     * */
    @Test
    public void test1(){
        Map<String, String> map = getInfo();
        map.putIfAbsent("4", "l");
        map.putIfAbsent("6", "f");
        System.out.println(map);
    }

    /**
     * 替换掉key值存在的value值,key值不存在则不操作
     * */
    @Test
    public void test2(){
        Map<String, String> map = getInfo();
        map.replace("2","666");
        map.replace("6","888");
        System.out.println(map);
    }

    /**
     * 将 hashMap 中的所有映射关系替换成给定的函数所执行的结果。
     * */
    @Test
    public void test3(){
        Map<String, String> map = getInfo();
        map.replaceAll((key,val)-> val+"----");
        System.out.println(map);
    }

    /**
     * 获取指定 key 对应对 value,如果找不到 key ,则返回设置的默认值。
     * */
    @Test
    public void test4(){
        Map<String, String> map = getInfo();
        System.out.println(map.getOrDefault("1", "not fond"));
        System.out.println(map.getOrDefault("5", "not fond"));
    }

    /**
     * 对 HashMap 中的每个映射执行指定的操作。
     * */
    @Test
    public void test5(){
        Map<String, String> map = getInfo();
        map.forEach((key,val)->{
            System.out.println(key+"*");
        });
        System.out.println(map);

    }

    /**
     * 返回此映射中包含的映射的 Set 视图。
	 * 注意:Set 视图意思是 HashMap 中所有的键值对都被看作是一个 set 集合。
     * */
    @Test
    public void test6(){
        Map<String, String> map = getInfo();
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for (Map.Entry<String, String> entry:entrySet) {
//            System.out.println(entry.getKey()+":"+entry.getValue());
            entry.setValue("666");
        }
        //值都更新了
        System.out.println(entrySet);
        //值都更新了
        System.out.println(map);

    }

    /**
     * 返回 hashMap 中所有 key 组成的集合视图。
     * */
    @Test
    public void test7(){
        Map<String, String> map = getInfo();
        Set<String> set = map.keySet();
        System.out.println(set);
    }

    /**
     * 返回 hashMap 中存在的所有 value 值。
     * */
    @Test
    public void test8(){
        Map<String, String> map = getInfo();
        List<String> values = new ArrayList<>(map.values());
        System.out.println(values);
    }

    /**
     * 会先判断指定的 key 是否存在,如果不存在,则添加键值对到 hashMap 中,存在添加函数计算的结果
     * */
    @Test
    public void test9(){
        Map<String, String> map = getInfo();
        String m = map.merge("5", "m", (oldValue, newValue) -> oldValue + "/" + newValue);
        System.out.println(m);
        String n = map.merge("2", "n", (oldValue, newValue) -> oldValue + "/" + newValue);
        System.out.println(n);
        System.out.println(map);

    }

    /**
     * key 对应的 value 不存在,则返回使用null进行计算的结果,如果存在,则返回值使用存在值进行计算的结果。
     * */
    @Test
    public void test10(){
        Map<String, String> map = getInfo();
        String s = map.compute("5", (key, value) -> key + value + "*");
        System.out.println(s);
        String s2 = map.compute("2", (key, value) -> key + value + "*");
        System.out.println(s2);
        System.out.println(map);

    }

    /**
     * 对 hashMap 中指定 key 的值进行重新计算,如果不存在这个 key,则添加到 hashMap 中。
     * */
    @Test
    public void test11(){
        Map<String, String> map = getInfo();
        String s = map.computeIfAbsent("2", key -> "666");
        System.out.println(s);
        String s2 = map.computeIfAbsent("5", key -> "666");
        System.out.println(s2);
        System.out.println(map);

    }

    /**
     * 对 hashMap 中指定 key 的值进行重新计算,前提是该 key 存在于 hashMap 中。
     * */
    @Test
    public void test12(){
        Map<String, String> map = getInfo();
        String s = map.computeIfPresent("2", (key, val) -> key + ":" + val);
        System.out.println(s);
        String s2 = map.computeIfPresent("5", (key, val) -> key + ":" + val);
        System.out.println(s2);
        System.out.println(map);

    }

    /**
     * 扩展
     * */
    @Test
    public void test13(){
        Map<String,HashMap> map = new HashMap<>(8);
        map.put("1",new LinkedHashMap<String,String>());
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("m","m");
        linkedHashMap.put("n","n");
        map.put("2",linkedHashMap);
        System.out.println("map:"+map);
        map.computeIfPresent("2",(key,val)->{
            System.out.println("map中键值是2的值:"+val);
            val.put("l","l");
            return val;
        });
        System.out.println("map:"+map);
        /**
         * 如果函数式接口方法返回null,会删除掉该键。
         * */
        map.computeIfPresent("2",(key,val)->{
            System.out.println("map中键值是2的值:"+val);
            return null;
        });
        System.out.println("map中键值是2计算后的值:"+map.get("2"));
        System.out.println("map:"+map);

    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值