1.putIfAbsent(不存在时执行put)
- 方法简介
 
英文翻译:如果不存在(absent),则put
即如果put的key值不存在就进行put,若key已经存在了,就不执行put操作
演示
@Testpublic void testPutIfAbsent(){HashMap<String, Integer> map = new HashMap<>(1);map.put("lulu",2);System.out.println("调用putIfAbsent方法前:"+map.get("lulu"));//由于key ="lulu" 已经有值了,所以value=10 put失败, value仍为 2Integer lulu = map.putIfAbsent("lulu", 10);System.out.println("方法返回值:"+lulu);System.out.println("调用putIfAbsent方法前:"+map.get("lulu"));}
2.getOrDefault(没有key获取默认值)
方法简介
如果key值不存在,则返回指定的默认值
演示
@Testpublic void testGetOrDefault(){HashMap<String,Integer> map = new HashMap<>();Integer lulu = map.getOrDefault("lulu", 10);System.out.println(lulu);}
3.merge(与当前value值合并)
演示
@Testpublic void testMerge(){Map<String,Integer> map= new HashMap<>(16);map.put("Java",1);map.merge("Java",10,(nowValue,bindValue)->{System.out.println("当前key=Java中存放的value值:"+nowValue); // 输出 1System.out.println("当前key=Java绑定的值,即第二个参数的值"+bindValue); //输出 10Integer returnValue = nowValue + bindValue;//如果return null 则等于删除这个keyreturn returnValue;});System.out.println(map.get("Java")); //输出 11}
方法简介
如上面的例子,如果 没有key=”Java” 的值,那么就 put(”Java”,10);
若存在key=”Java”的值,且value=nowValue,那么就走一个lamda表达式,返回一个值作为最终的”Java”的value。
上面演示中的merge写法就相当于下面这种:
@Testpublic void testMerge(){final Integer bindValue = 10;Map<String,Integer> map= new HashMap<>(16);map.put("Java",1);if(map.containsKey("Java")){Integer nowValue = map.get("Java");Integer returnValue = nowValue + bindValue;map.put("Java",returnValue);}else {map.put("Java",bindValue);}System.out.println(map.get("Java")); //输出 11}
4.compute(根据key-value计算新的value)
演示
@Testpublic void testComputer(){Map<String,Integer> map= new HashMap<>(16);map.put("Java",1);//返回一个新vlaueInteger newValue = map.compute("Java", (key, value) -> {String thisKey = key;Integer thisValue = value;//如果这步 return null 则 ==> 删除 key='Java'return thisKey.length() + thisValue;});System.out.println(newValue); //输出 5System.out.println(map.get("Java")); //输出 5}
方法简介
这个方法的作用是将 一对key-value值中 的 key 和value值组合,来生成一个新的 value值 。
5.computeIfPresent(存在key时才根据key和vlaue计算新value)
- 演示
 
如果使用 computer :
@Testpublic void testComputer(){Map<String,Integer> map= new HashMap<>(16);Integer newValue = map.compute("Java", (key, value) -> {String thisKey = key;//如果这步 return null 则 ==> 删除 key='Java'return thisKey.length();});System.out.println(newValue); //输出 4System.out.println(map.get("Java")); //输出 4}
使用computerIfPresent:
@Testpublic void testComputer(){Map<String,Integer> map= new HashMap<>(16);Integer newValue = map.computeIfPresent("Java", (key, value) -> {String thisKey = key;//如果这步 return null 则 ==> 删除 key='Java'return thisKey.length();});System.out.println(newValue); //输出 nullSystem.out.println(map.get("Java")); //输出 null}
- 方法简介
 
与computer不同在于,如果key不存在,computerIfPresent压根就不走入第二个lamda表达式。
6.computerIfAbsent (不存在key时才根据key计算新value)
演示
@Testpublic void testComputerIfAbsent(){Map<String,Integer> map = new HashMap<>();map.computeIfAbsent("Java",(key)->key.length());System.out.println(map.get("Java")); //输出 4}
方法简介
当key值在map中不存在时,通过提供的lamda表达式计算value,之后执行 将key和value放入map
7.replaceAll(替换所有的key-value)
- 方法简介
 
此方法遍历每一对key value,并将value置为所返回的值
- 演示
@Testpublic void testReplaceAll(){Map<String,Integer> map = new HashMap<>();map.put("Java",10);map.put("Hadoop",1);map.put("Hive",11);map.put("Spark",100);//将所有的key的value都置为1map.replaceAll((key,value)->{System.out.println("key为:"+key);System.out.println("value为:"+value);return 1;});//遍历mapmap.forEach((key,value)->{System.out.println(key+"="+value);});/*** Java=1* Hive=1* Hadoop=1* Spark=1*/}
 
