package com.zyp.test;
import com.google.common.collect.Maps;
import java.util.HashMap;
/**
*put():key不存在则直接插入,返回值为null,key存在则对应的value值被新值覆盖,返回值为旧值
*compute():key不存在则直接插入,返回值为新值,key存在时,则通过remappingFunction计算新的value,返回计算后的value值,key值不变
*computeIfAbsent():key不存在则value进行mappingFunction运算(i -> i + "a",i为key值),返回值新值,key存在,value不变,返回值旧值
*computeIfPresent():key不存在则不插入,返回值为null,key存在时value进行remappingFunction,返回值为新值
*merge():key不存在时,则把第二个参数当做value插入到map中,返回第二个参数,key存在时,
*对原有value值和第二个参数值进行remappingFunction,返回计算后的value
*/
public class Map18 {
public static void main(String[] args) {
HashMap<Integer, String> map = Maps.newHashMap();
//key不存在则直接插入,返回值为null,key存在则对应的value值被新值覆盖,返回值为旧值
String put = map.put(1, "1");
System.out.println("put->"+put);
String put1 = map.put(1, "2");
System.out.println("put1->"+put1);
//key不存在则直接插入,返回值为新值,key存在时,则通过remappingFunction计算新的value,返回计算后的value值,key值不变
String compute = map.compute(2, (K, V) -> "2");
System.out.println("compute->"+compute);
String compute1 = map.compute(2, (K, V) -> V+"b");
System.out.println("compute1->"+compute1);
//key不存在则value进行mappingFunction运算(i -> i + "a",i为key值),返回值新值,key存在,value不变,返回值旧值
String computeIfAbsent = map.computeIfAbsent(2, i -> i + "a");
System.out.println("computeIfAbsent->"+computeIfAbsent);
String computeIfAbsent2 = map.computeIfAbsent(4, i -> i + "a");
System.out.println("computeIfAbsent2->"+computeIfAbsent2);
System.out.println(map);
//key不存在则不插入,返回值为null,key存在时value进行remappingFunction,返回值为新值
String computeIfPresent = map.computeIfPresent(4, (K, V) -> V + "a");
System.out.println("computeIfPresent->"+computeIfPresent);
String computeIfPresent2 = map.computeIfPresent(5, (K, V) -> V + "a");
System.out.println("computeIfPresent2->"+computeIfPresent2);
System.out.println(map);
//key不存在时,则把第二个参数当做value插入到map中,返回第二个参数,
//key存在时,对原有value值和第二个参数值进行remappingFunction,返回计算后的value
String merge = map.merge(5, "5", (old, new1) -> old + new1);
System.out.println("merge->"+merge);
String merge2 = map.merge(5, "6", (old, new1) -> old + new1);
System.out.println("merge2->"+merge2);
String merge1 = map.merge(6, "6", (old, new1) -> old + new1);
System.out.println("merge1->"+merge1);
System.out.println(map);
}
}
使用举例:
package com.zyp.test;
import com.google.common.collect.Maps;
import java.util.HashMap;
/**
* @author syl
* @description TODO
* @since 2022/3/8
*/
public class MapTest {
public static void main(String[] args) {
//统计随机字符串中每个字符的个数
String a="fiensdkajfhquebdjsankroiwqfhusdbvjnkwoqdas";
//方法1
HashMap<Character, Integer> map = Maps.newHashMap();
for (int i = 0; i <a.length() ; i++) {
char c = a.charAt(i);
Integer value = map.get(c);
if(null==value){
value=1;
}else{
++value;
}
map.put(c, value);
}
System.out.println("map->"+map);
System.out.println();
map.clear();
//方法2
for (int i = 0; i < a.length(); i++) {
map.compute(a.charAt(i),(K,V)->{
if(V==null){
V=1;
}else{
++V;
}
return V;
});
}
System.out.println("map1->"+map);
//方法3
map.clear();
for (int i = 0; i < a.length(); i++) {
map.merge(a.charAt(i), 1, (s,s1)->s+s1);
}
System.out.println("map2->"+map);
}
}