zoukankan      html  css  js  c++  java
  • 你只会用 map.put?试试 Java 8 compute ,操作 Map 更轻松!

    今天分享一个实用的 Java 8 开发技能,那就是 Map 接口中增加的 compute 方法,给 Map 集合计算更新用的。

    compute简介

    如下所示,Java 8 在 Map 和 ConcurrentMap 接口中都增加了 3 个 compute 方法,说明也是支持多线程并发安全操作的。

     

    这三个方法的区别:

    1. compute:计算并更新值

    2. computeIfAbsent:Value不存在时才计算

    3. computeIfPresent:Value存在时才计算

    先看看没用 Java 8 的一个小示例:

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MapTest {
        public static void main(String[] args) {
    
            List<String> animals = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");
            Map<String, Integer> map = new HashMap<>();
            for (String animal : animals) {
                Integer count = map.get(animal);
                map.put(animal, count == null ? 1 : ++count);
            }
            System.out.println("map = " + map);
        }
    }

    输出:  map = {cat=2, fish=1, dog=3}

    这是一个统计一个列表中每个动物的数量,代码再怎么精简都需要一步 get 操作,判断集合中是否有元素再确定是初始化:1,还是需要 +1。

    很多时候,这个 get 操作显然是毫无必要的,所以 Java 8 提供了 3 个 compute 方法,来看看怎么用吧!

    Java 8 compute 实现方式:

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MapTest {
        public static void main(String[] args) {
    
            List<String> animalList = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");
            Map<String, Integer> maps = new HashMap<>();
            for (String animal : animalList) {
                maps.compute(animal, (k, v) -> v == null ? 1 : ++v);
            }
            System.out.println("maps = " + maps);
        }
    }

    输出:  maps = {fish=1, cat=2, dog=3}

    使用 compute 方法一行搞定,省去了需要使用 get 取值再判断的冗余操作,直接就可以获取元素值并计算更新,是不是很方便呢?

    compute源码分析

    这还是一个默认方法,为什么是默认方法,也是为了不改动其所有实现类。

    default V compute(K key,
                          BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
    
            // 函数式接口不能为空
            Objects.requireNonNull(remappingFunction);
    
            // 获取旧值
            V oldValue = get(key);
    
            // 获取计算的新值
            V newValue = remappingFunction.apply(key, oldValue);
    
            // 新值为空
            if (newValue == null) {
                // delete mapping
                // 旧值存在时
                if (oldValue != null || containsKey(key)) {
                    // something to remove
                    // 移除该键值
                    remove(key);
                    return null;
                } else {
                    // nothing to do. Leave things as they were.
                    return null;
                }
            } else { // 新值不为空
                // add or replace old mapping
                // 添加或者覆盖旧值
                put(key, newValue);
                return newValue;
            }
        }

    实现逻辑其实也很简单,其实就是结合了 Java 8 的函数式编程让代码变得更简单了,Java 也越来越聪明了。

    另外两个方法就不演示了,在特定的场合肯定也肯定特别有用,大家知道就好,需要的时候要知道拿来用。

    完整代码及测试结果:

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MapTest {
        public static void main(String[] args) {
    
            List<String> animals = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");
            Map<String, Integer> map = new HashMap<>();
            for (String animal : animals) {
                Integer count = map.get(animal);
                map.put(animal, count == null ? 1 : ++count);
            }
            System.out.println("map = " + map);
    
            List<String> animalList = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");
            Map<String, Integer> maps = new HashMap<>();
            for (String animal : animalList) {
                maps.compute(animal, (k, v) -> v == null ? 1 : ++v);
            }
            System.out.println("maps = " + maps);
        }
    }

     

  • 相关阅读:
    XPSP2 PSDK(还有lostspeed)
    给c++程序员的一份礼物——常用工具集
    setStyleSheet来设定窗口部件的样式
    Guava学习笔记:Immutable(不可变)集合
    迷你MVVM框架 avalonjs 0.82发布
    看到他我一下子就悟了-- 反射
    C# Socket编程
    Exchange Server 2013 一步步安装图解
    编码标准之格式
    Drupal与大型网站架构(译)- Large-Scale Web Site Infrastructure and Drupal
  • 原文地址:https://www.cnblogs.com/liyhbk/p/15271062.html
Copyright © 2011-2022 走看看