1. 如果value不为空,进行merge处理。为空直接赋值newValue。实际上就是一个put操作。 然后将key对应的oldValue和newValue 根据传来的function进行处理。
2 当处理后的value值为空,map会移除当前key
原码如下:
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);
//1.给的key值的value为空,赋值newValue. 如果给的key值的value不为空,使用传过来的函数处理 oldValue和 newValue
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
//如果处理后的newValue为空,移除当前值,因传入的函数可能返回null值
if(newValue == null) {
remove(key);
} else {
//如果处理后的newValue为不空, put操作
put(key, newValue);
}
return newValue;
}
使用案例:
import java.util.*;
import java.util.function.BiFunction;
/**
* @author wangkebing
* @Description
* @date
* 测试merge方法,计算每个学生的总分
*/
public class MapMergeTest {
public static void main(String[] args) {
List<StudentScore> list = new ArrayList<>();
list.add(new StudentScore(1, "chinese", 110));
list.add(new StudentScore(1, "english", 120));
list.add(new StudentScore(1, "math", 135));
list.add(new StudentScore(2, "chinese", 99));
list.add(new StudentScore(2, "english", 100));
list.add(new StudentScore(2, "math", 133));
list.add(new StudentScore(3, "chinese", 88));
list.add(new StudentScore(3, "english", 140));
list.add(new StudentScore(3, "math", 90));
list.add(new StudentScore(4, "chinese", 108));
list.add(new StudentScore(4, "english", 123));
list.add(new StudentScore(4, "math", 114));
list.add(new StudentScore(5, "chinese", 116));
list.add(new StudentScore(5, "english", 133));
list.add(new StudentScore(5, "math", 135));
System.out.println(sum1(list));
System.out.println(sum2(list));
}
//传统写法
public static Map<Integer, Integer> sum1(List<StudentScore> list) {
Map<Integer, Integer> map = new HashMap<>();
for (StudentScore studentScore : list) {
if (map.containsKey(studentScore.getSid())) {
map.put(studentScore.getSid(),
map.get(studentScore.getSid()) + studentScore.getScore());
} else {
map.put(studentScore.getSid(), studentScore.getScore());
}
}
return map;
}
//merger写法
public static Map<Integer, Integer> sum2(List<StudentScore> list) {
Map<Integer, Integer> map1 = new HashMap<>();
Map<Integer, Integer> map2 = new HashMap<>();
Map<Integer, Integer> map3 = new HashMap<>();
Map<Integer, Integer> map4 = new HashMap<>();
Map<Integer, Integer> map5 = new HashMap<>();
//Integer::sum 这是lamda表达式的写法 调用Integer的sum方法
list.stream().forEach(studentScore -> map1.merge(studentScore.getSid()
, studentScore.getScore(), Integer::sum));
list.stream().forEach(studentScore -> map2.merge(studentScore.getSid(),studentScore.getScore(),(v1,v2)->v1+v2));
list.stream().forEach(studentScore -> map3.merge(studentScore.getSid(),studentScore.getScore(),(v1,v2)-> v2));
//这里可以看出 (v1,v2)-> v2) lamda表达式是对Bifunction的匿名内部类实现
list.stream().forEach(studentScore -> map4.merge(studentScore.getSid(),studentScore.getScore(), new BiFunction(){
@Override
public Object apply(Object o, Object o2) {
return (Integer)o+(Integer)o2;
}
}) );
list.stream().forEach(studentScore -> map5.merge(studentScore.getSid(),studentScore.getScore(),(v1,v2)->222) );
System.out.println("----------------------");
System.out.println(map1);
System.out.println(map2);
System.out.println(map3);
System.out.println(map4);
System.out.println(map5);
System.out.println("----------------------");
return map1;
}
static class StudentScore {
private Integer sid;
private String scoreName;
private Integer score;
public StudentScore(Integer sid, String scoreName, Integer score) {
this.sid = sid;
this.scoreName = scoreName;
this.score = score;
}
public StudentScore() {
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getScoreName() {
return scoreName;
}
public void setScoreName(String scoreName) {
this.scoreName = scoreName;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
}
}