Map一些新方法的具体使用案例
1、getOrDefault:default V getOrDefault(Object key, V defaultValue)
package com.qbb.threadpool;
import java.util.HashMap;
import java.util.Map;
/**
* @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
* @version 1.0
* @date 2022-07-23 11:49
* @Description:
*/
public class Java8NewFeature {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("name", "qiuqiu");
map.put("age", "22");
map.put("hobby", "haha");
map.put("interest", "no");
/**
* getOrDefault
*/
String orDefaultValue = getOrDefaultTest(map);
System.out.println("orDefaultValue = " + orDefaultValue);
}
/**
* getOrDefault
* 如果Map中不存在该key,可以提供一个默认值,方法会返回改默认值。
* 如果存在该key,返回键对应的值
*/
public static String getOrDefaultTest(Map<String,String> map){
String orDefault = map.getOrDefault("qiu", "baoabo");
return orDefault;
}
}

2、forEach:default void forEach(BiConsumer<? super K, ? super V> action)
/**
* forEach
* 遍历map
*/
public static String forEachTest(Map<String, String> map) {
/**
* 以前的写法
*/
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "===" + value);
}
/**
* 现在的写法
*/
map.forEach((k, v) -> System.out.println(k + "===" + v));
/**
* 还可以做一些中间操作
*/
map.forEach((k, v) -> {
System.out.println(k + "===" + v);
v = v + "you";
map.put(k, v);
});
map.forEach((k, v) -> System.out.println(k + "===" + v));
return null;
}

3、putIfAbsent:default V putIfAbsent(K key, V value)
/**
* putIfAbsent
* V putIfAbsent(K key, V value)只有在不存在key值的映射或者映射值为null,才将value值赋值给key。
* 否则不做修改。该方法将条件判断和赋值合二为一。
*/
public static String putIfAbsentTest(Map<String, String> map) {
String result1 = map.put("name", "ll");
System.out.println("result1 = " + result1);
String result2 = map.putIfAbsent("name", "qiuqiu");
System.out.println("result2 = " + result2);
String name = map.get("name");
System.out.println("name = " + name);
String absent = map.putIfAbsent("hello", "qiuqiu");
System.out.println("absent = " + absent);
String hello = map.get("hello");
System.out.println("hello = " + hello);
return null;
}

4、compute:default V compute(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction)
default V compute(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
// 获取key对应的值
V oldValue = get(key);
//获取新值
V newValue = remappingFunction.apply(key, oldValue);
// 如果新值为null,并且key存在,则删除key;否则把新值赋值给key
if (newValue == null) {
// delete mapping
if (oldValue != null || containsKey(key)) {
// something to remove
remove(key);
return null;
} else {
// nothing to do. Leave things as they were.
return null;
}
} else {
// add or replace old mapping
put(key, newValue);
return newValue;
}
}
5、computeIfAbsent:default V computeIfAbsent(K key,Function<? super K, ? extends V> mappingFunction)
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("name", "qiuqiu");
map.put("age", "22");
map.put("hobby", "haha");
map.put("interest", "no");
/*
当Map中不存在key值的映射或映射值为null时,调用mappingFunction,
并在mappingFunction执行结果非null时,将结果赋值给key。
*/
String ifAbsent = map.computeIfAbsent("qiuqiu", k -> {
return "hello:" + k;
});
System.out.println("ifAbsent = " + ifAbsent);
}

6、merge:default V merge(K key, V value,BiFunction<? super V, ? super V, ? extends V> remappingFunction)
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("name", "qiuqiu");
map.put("age", "22");
map.put("hobby", "haha");
map.put("interest", "no");
/**
* value和remappingFunction不能为null
* 如果Map中key对应的映射不存在或者为null,
* 则将value关联到key上;否则执行remappingFunction,
* 如果执行结果为null则删除key的映射,否则用该结果跟key关联。
*/
// 给存在的key进行映射处理
String merge = map.merge("name", "ll", (k, v) -> {
System.out.println(k + "===" + v);
return k + v;
});
System.out.println("merge = " + merge);
// 给不存在的key,不会执行
String result = map.merge("haha", "ll", (k, v) -> {
System.out.println(k + "===" + v);
return k + v;
});
System.out.println("map.get(haha) = " + map.get("haha"));
System.out.println("result = " + result);
}
7、remove(key,value):default boolean remove(Object key, Object value)
/**
* remove
* 只有在当前Map中key映射的值等于value时才删除该映射,否则什么也不做。
*/
public static String removeTest(Map<String, String> map) {
boolean flag = map.remove("name", "qiuqiu");
System.out.println("flag = " + flag);
boolean flag2 = map.remove("hobby", "ll");
System.out.println("flag2 = " + flag2);
return null;
}

8、replace:default boolean replace(K key, V oldValue, V newValue)
/**
* replace
*/
public static String replaceTest(Map<String, String> map){
String one = map.replace("name", "haha");
System.out.println("one = " + one);
String two = map.replace("haha", "hehe");
System.out.println("two = " + two);
boolean flag = map.replace("name", "haha", "hehe");
System.out.println("flag = " + flag);
return null;
}

9、replaceAll:default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("name", "qiuqiu");
map.put("age", "22");
map.put("hobby", "haha");
map.put("interest", "no");
map.replaceAll((k, v) -> {
// 对 k v 进行处理
if ("hobby".equals(k)) {
return v.toUpperCase();
}
return v;
});
// 打印
map.forEach((k, v) -> System.out.println(k + "=" + v));
}

当然还有其他的方法就不一一列举了,大家可以去试一下