1,getOrDefault
只要 map.keySet() 中含有该 key, 就会返回对应的 value, 即使 value == null
default V getOrDefault(Object key, V defaultValue) {
V v;
return (((v = get(key)) != null) || containsKey(key)) ? v : defaultValue;
}
使用示例:
public class Java8MapTest {
public static void main(String[] args){
Map<String, String> map = new HashMap<>();
map.put("AAA", "111");
map.put("BBB", "222");
map.put("NNN", null);
System.out.println(map);//{AAA=111, BBB=222, NNN=null}
/**
* public V getOrDefault(Object key, V defaultValue)
* 使用getOrDefault的时候要注意,当map中包含对应的 key, 即使 value == null, 也会直接返回 null, 而不会使用默认值
*/
System.out.println(map.getOrDefault("AAA", "default-value"));//111
System.out.println(map.getOrDefault("CCC", "default-value"));//default-value
System.out.println(map.getOrDefault("NNN", "default-value"));//null
}
}
2 , putIfAbsent
当 map.keySet() 不含有该 key, 或者包含有该 key ,但对应的 value == null 时,才会进行添加操作
default V putIfAbsent(K key, V value) {
V v = get(key);
if (v == null) {
v = put(key, value);
}
return v;
}
使用示例:
public class Java8MapTest {
public static void main(String[] args){
Map<String, String> map = new HashMap<>();
map.put("AAA", "111");
map.put("BBB", "222");
map.put("NNN", null);
System.out.println(map);//{AAA=111, BBB=222, NNN=null}
/**
* public V putIfAbsent(K key, V value)
* 使用putIfAbsent的时候要注意,当map中包含对应的 key, 如果 value == null, 则也会进行 put() 操作
*/
map.putIfAbsent("AAA", "333");
System.out.println(map); //{AAA=111, BBB=222, NNN=null}
map.putIfAbsent("DDD", "444");
System.out.println(map); //{AAA=111, BBB=222, DDD=444, NNN=null}
map.putIfAbsent("NNN", "putIfAbsent");
System.out.println(map); //{AAA=111, BBB=222, DDD=444, NNN=putIfAbsent}
}
}
3 , remove
使用removw(k,v)的时候要注意,只有 key:value 与 map 中的 key:value 完全对应时,才会被移除
default boolean remove(Object key, Object value) {
Object curValue = get(key);
if (!Objects.equals(curValue, value) ||
(curValue == null && !containsKey(key))) {
return false
}
remove(key);
return true;
}
使用示例:
public class Java8MapTest {
public static void main(String[] args){
Map<String, String> map = new HashMap<>();
map.put("AAA", "111");
map.put("BBB", "222");
map.put("NNN", null);
System.out.println(map);//{AAA=111, BBB=222, NNN=null}
/**
* public boolean remove(Object key, Object value)
* 使用removw(k,v)的时候要注意,只有 key:value 与 map 中的 key:value 完全对应时,才会被移除
*/
map.remove("AAA", "555");
System.out.println(map);//{AAA=111, BBB=222, NNN=null}
map.remove("AAA", "111");
System.out.println(map);//{BBB=222, NNN=null}
map.remove("NNN", "666");
System.out.println(map);//{BBB=222, NNN=null}
map.remove("NNN", null);
System.out.println(map);//{BBB=222}
}
}
4 , replace
replace有两个方法,replace(k,v)直接替换,返回当前值;replace(k,v1,v2)只在当key,value都对应的时候,才会进行替换
default V replace(K key, V value) {
V curValue;
if (((curValue = get(key)) != null) || containsKey(key)) {
curValue = put(key, value);
}
return curValue;
}
default boolean replace(K key, V oldValue, V newValue) {
Object curValue = get(key);
if (!Objects.equals(curValue, oldValue) ||
(curValue == null && !containsKey(key))) {
return false;
}
put(key, newValue);
return true;
}
使用示例:
public class Java8MapTest {
public static void main(String[] args){
Map<String, String> map = new HashMap<>();
map.put("AAA", "111");
map.put("BBB", "222");
map.put("NNN", null);
System.out.println(map);//{AAA=111, BBB=222, NNN=null}
/**
* public V replace(K key, V value)
* public V replace(K key, V oldValue, V newValue)
* replace(k,v),当containsKey(k)时,进行替换,返回当前值
* replace(k,v1,v2), 当containsKey(k)时, curValue.equals(v1) == true 时, 才进行替换
*/
map.replace("NNN", "new null");
System.out.println(map);//{AAA=111, BBB=222, NNN=new null}
map.replace("AAA","333", "new 111");
System.out.println(map);//{AAA=111, BBB=222, NNN=new null}
map.replace("AAA","111", "new 111");
System.out.println(map);//{AAA=new 111, BBB=222, NNN=new null}
}
}
5 , compute
利用函数计算的结果,作为value替换或者新增 key:value
default V compute(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
V oldValue = get(key);
V newValue = remappingFunction.apply(key, oldValue);
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;
}
}
使用示例:
public class Java8MapTest {
public static void main(String[] args){
Map<String, String> map = new HashMap<>();
map.put("AAA", "111");
map.put("BBB", "222");
map.put("NNN", null);
System.out.println(map);//{AAA=111, BBB=222, NNN=null}
//BiFunction
BiFunction<String, String, String> BiF = new BiFunction<String, String, String>() {
@Override
public String apply(String s, String s2) {
if(s2 != null){
return "new "+ s2;
}else{
return "the value is null";
}
}
};
/**
* map.compute(K key, BiFunction)
* 利用Bi函数计算的结果,作为value替换或者新增 key:value 对
*/
map.compute("AAA", BiF);
System.out.println(map);//{AAA=new 111, BBB=222, NNN=null}
map.compute("NNN", BiF);
System.out.println(map);//{AAA=new 111, BBB=222, NNN=the value is null}
}
}
6 , computeIfPresent 和 computeIfAbsent
default V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
V oldValue;
if ((oldValue = get(key)) != null) {
V newValue = remappingFunction.apply(key, oldValue);
if (newValue != null) {
put(key, newValue);
return newValue;
} else {
remove(key);
return null;
}
} else {
return null;
}
}
default V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(mappingFunction);
V v;
if ((v = get(key)) == null) {
V newValue;
if ((newValue = mappingFunction.apply(key)) != null) {
put(key, newValue);
return newValue;
}
}
return v;
}
使用示例:
public class Java8MapTest {
public static void main(String[] args){
Map<String, String> map = new HashMap<>();
map.put("AAA", "111");
map.put("BBB", "222");
map.put("NNN", null);
System.out.println(map);//{AAA=111, BBB=222, NNN=null}
//BiFunction
BiFunction<String, String, String> BiF = new BiFunction<String, String, String>() {
@Override
public String apply(String s, String s2) {
if(s2 != null){
return "new "+ s2;
}else{
return "the value is null";
}
}
};
/**
* map.computeIfPresent(K key, BiFunction)
* 当get(key)不为null时,利用Bi函数计算的结果,作为value替换旧的value, 返回旧值; 反之,直接返回 null
*/
map.computeIfPresent("AAA", BiF);
System.out.println(map);//{AAA=new 111, BBB=222, NNN=null}
map.computeIfPresent("NNN", BiF);
System.out.println(map);//{AAA=new 111, BBB=222, NNN=null}
/**
* map.computeIfAbsent(K key, function(k))
* 当get(key)为null时,利用函数计算的结果,作为value新增或者替换旧的map.entry(k,v)
*/
map.computeIfAbsent("AAA", k -> {return k+"value";});
System.out.println(map);//{AAA=new 111, BBB=222, NNN=null}
map.computeIfAbsent("CCC", k -> {return k+"value";});
System.out.println(map);//{AAA=new 111, CCC=CCCvalue, BBB=222, NNN=null}
map.computeIfAbsent("NNN", k -> {return k+"value";});
System.out.println(map);//{AAA=new 111, CCC=CCCvalue, BBB=222, NNN=NNNvalue}
}
}
7 , merge
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);
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
if(newValue == null) {
remove(key);
} else {
put(key, newValue);
}
return newValue;
}
使用示例:
public class Java8MapTest {
public static void main(String[] args){
Map<String, String> map = new HashMap<>();
map.put("AAA", "111");
map.put("BBB", "222");
map.put("NNN", null);
System.out.println(map);//{AAA=111, BBB=222, NNN=null}
//BiFunction
BiFunction<String, String, String> BiF = new BiFunction<String, String, String>() {
@Override
public String apply(String s, String s2) {
if(s2 != null){
return s +"-"+ s2;
}else{
return "the value is null";
}
}
};
/**
* map.merge(K key, V V, BiFunction BiF)
* 当get(key)不为null时,以get(key), v 作为参数计算返回结果,如果结果为null,则remove(k),反之则替换旧值
* 当get(key)为 null 时,直接用 v 进行替换
*/
map.merge("AAA", "999", BiF);
System.out.println(map);//{AAA=111-999, BBB=222, NNN=null}
map.merge("NNN", "nnn999", BiF);
System.out.println(map);//{AAA=111-999, BBB=222, NNN=nnn999}
}
}