因在看部分源码时,发现用到一些Map的新增方法但是并不知道其具体含义,故了解并整理。
简单点说就是针对key的value重新设值,具体的值取决于自定义函数返回值。 自定义函数返回值不等于空,则会将自定义函数返回的value设置成key对应的值。 如果key对应的value不等于空但是自定义函数返回值等于空则会删除这个key。
V oldValue = map.get(key); // 通过自定义的BiFunction,对key和oldValue处理后返回新的数据 V newValue = remappingFunction.apply(key, oldValue); // 如果oldValue不为空 if (oldValue != null ) { // 并且自定义的的BiFunction返回的newValue不等于空 if (newValue != null) // 替换直接的oldValue map.put(key, newValue); else // 如果自定义的的BiFunction返回的newValue等于空,则移除该key map.remove(key); } else { // 如果oldValue等于空,newValue不等于空,则存放key,newValue到map中 if (newValue != null) map.put(key, newValue); else // 否则返回null return null; }统计一个字符串中字符出现的次数
String str = "hello, i am vary happy! nice to meet you, my name is demo"; // jdk8之前的写法 HashMap<Character, Integer> result1 = new HashMap<>(32); for (int i = 0; i < str.length(); i++) { char curChar = str.charAt(i); Integer curVal = result1.get(curChar); if (curVal == null) { curVal = 1; } else { curVal += 1; } result1.put(curChar, curVal); } result1.forEach((k, v) -> { System.out.println(k + "--" + v); }); // 清除信息 result1.clear(); // jdk8的写法 for (int i = 0; i < str.length(); i++) { char curChar = str.charAt(i); result1.compute(curChar, (key, oldValue) -> { return oldValue == null ? 1 : oldValue + 1; }); } // map中的forEach使用 result1.forEach((k, v) -> { System.out.println(k + "--" + v); });简单点说:如果key对应的value存在就计算,如果key对应的value不存在就不处理。如果key对应的value存在,但是函数返回null,则会移除该key。
//如果key对应的value存在就处理,不存在不处理 if (map.get(key) != null) { // 获取该key对应的value V oldValue = map.get(key); // 通过自定义的BiFunction函数,返回新的value V newValue = remappingFunction.apply(key, oldValue); // 如果新的value不等于空,则将key的value设置成新的value,否则移除 if (newValue != null) map.put(key, newValue); else map.remove(key); }处理map中的数字减
// jdk8之前的 public static void reduce1() { Map<String, Integer> map = new HashMap<>(); // 如果1对应的值存在则进行计算,这里不会计算 Integer integer = map.get("1"); //如果没有这个判断,会报错 if (integer == null) { return; } integer -= 1; } // jdk8版本 public static void reduce2() { Map<String, Integer> map = new HashMap<>(); map.computeIfPresent("1", (key, oldValue) -> { return oldValue -= 1; }); }简单点说当key对应的value没有的时候执行自定义函数并且将自定义函数返回值设置成value。
if (map.get(key) == null) { // 通过自定义的BiFunction函数返回数据 V newValue = mappingFunction.apply(key); // 如果不是null,则设置 if (newValue != null) map.put(key, newValue); }读取字典并存放到对应的map结构中
//jdk8之前的代码 Map<String, Map<String, String>> newMap = new HashMap<>(); Map<String, String> value = newMap.get("productUnit"); if(value==null){ value = new HashMap<>(); }else{ value.put("he","she"); } //jdk8代码 newMap = new HashMap<>(); //如果不存在,则创建一个map;存在则不会执行该自定义函数 newMap.computeIfAbsent("productUnit", key -> new HashMap<>()); newMap.get("productUnit").put("he", "she");简单点描述:如果key对应的value不存在,则将第二个参数的value设置成value。 如果key对应的value存在,则将自定义BiFunction函数返回值设置成value。
// 获取key对应的value V oldValue = map.get(key); // 如果value等于空,则返回设置的value,如果不等于空,则返回自定义函数的返回值 V newValue = (oldValue == null) ? value : remappingFunction.apply(oldValue, value); // 如果新的value等于空,则移除该key,否则将新的value存放到对应的key中 if (newValue == null) map.remove(key); else map.put(key, newValue);统计学生的总成绩
public class Score { public Integer sid; public String scoreName; public Integer score; public Score(Integer sid, String scoreName, Integer score) { this.sid = sid; this.scoreName = scoreName; this.score = score; } } //要统计每个学生的总成绩, List<Score> list = new ArrayList<>(); list.add(new Score(1, "chinese", 100)); list.add(new Score(1, "english", 103)); list.add(new Score(1, "math", 32)); list.add(new Score(2, "english", 65)); list.add(new Score(2, "chinese", 53)); System.out.println(sum1(list)); System.out.println(sum2(list)); System.out.println(sum3(list));采用流处理
public static Map<Integer, Integer> sum3(List<StudentScore> list) { Map<Integer, Integer> map = new HashMap<>(); list.stream().forEach(studentScore -> map.merge(studentScore.sid, studentScore.score, Integer::sum)); return map; }简单点说:更新value为空的key,否则不更新。
V v = map.get(key); // 如果value等于空,设置传递的value到map中 if (v == null) v = map.put(key, value); // 否则返回value return v;暂未想到
简单点说:可以将满足条件的key或者value的entry对应的value更新。
for (Map.Entry<K, V> entry : map.entrySet()) // 将该entry的value更新为自定义函数BiFunction的返回值 entry.setValue(function.apply(entry.getKey(), entry.getValue()));暂未想到