Map.merge() method usage and introduction

1 Introduction

The Map.merge() method is new in JDK8, but it has not been used before. merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction).

  1. key:map's key
  2. value: the value passed in by the user
  3. remappingFunction: BiFunction functional interface

When the specified key does not exist in the map, the incoming value is set to the value of the key, which is equivalent to map.put(key, value); when the key exists, a method is executed. This method receives the old value of the key and The value passed in, executes the custom method and returns the final result set to the value of the key.

2 source code

default V merge(K key, V value,
            BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
    
    
        Objects.requireNonNull(remappingFunction);
        Objects.requireNonNull(value);
        V oldValue = get(key);
        V newValue = (oldValue == null) ? value :
                   remappingFunction.apply(oldValue, value);
        if(newValue == null) {
    
    
            remove(key);
        } else {
    
    
            put(key, newValue);
        }
        return newValue;
    }

3 examples

3.1 Simple example

/**
     * 简单实例
     */
    @Test
    public void someTest01() {
    
    
        Map<String, Integer> map = new HashMap<>();
        map.put("key1", 10);
        map.put("key2", 20);
        // 当key存在时,进行old+value;当key不存在时,将key的值设置为1
        map.merge("key", 1, Integer::sum);
        // 当key1存在时,进行old+value;当key1不存在时,将key1的值设置为20
        map.merge("key1", 20, Integer::sum);
        map.forEach((k, v) -> System.out.println("k:" + k + "---" + "v:" + v));
    }
    // ------------------------------------------------------------------
    // 打印结果
    k:key1---v:30
    k:key2---v:20
    k:key---v:1

3.2 Development examples

@Data
public class Student {
    
    

    /**
     * 学生姓名
     */
    private String studentName;

    /**
     * 学科
     */
    private String subject;

    /**
     * 分数
     */
    private Integer score;
    
    public Student(String studentName, String subject, Integer score) {
    
    
        this.studentName = studentName;
        this.subject = subject;
        this.score = score;
    }
}
// ------------------------------------------------------------------
    /**
     * 统计学生成绩
     */
    @Test
    public void someTest02() {
    
    
        List<Student> students = Arrays.asList(
                new Student("yoni", "English", 80),
                new Student("yoni", "Chiness", 98),
                new Student("yoni", "Math", 95),
                new Student("taohai.wang", "English", 50),
                new Student("taohai.wang", "Chiness", 72),
                new Student("taohai.wang", "Math", 41),
                new Student("Seely", "English", 88),
                new Student("Seely", "Chiness", 89),
                new Student("Seely", "Math", 92)
        );
        Map<String, Integer> scoreMap = new HashMap<>();
        students.forEach(student -> scoreMap.merge(student.getStudentName(), student.getScore(), Integer::sum));
        scoreMap.forEach((k, v) -> System.out.println("key:" + k + "---" + "value:" + v));
    }
// ------------------------------------------------------------------
// 运行结果
姓名:Seely---成绩:269
姓名:taohai.wang---成绩:163
姓名:yoni---成绩:273

Guess you like

Origin blog.csdn.net/qq_37284798/article/details/132312003