Map一些新方法的具体使用案例
1、getOrDefault:default V getOrDefault(Object key, V defaultValue)
package com. qbb. threadpool ;
import java. util. HashMap ;
import java. util. Map ;
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" ) ;
String orDefaultValue = getOrDefaultTest ( map) ;
System . out. println ( "orDefaultValue = " + orDefaultValue) ;
}
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)
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)
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) ;
V oldValue = get ( key) ;
V newValue = remappingFunction. apply ( key, oldValue) ;
if ( newValue == null ) {
if ( oldValue != null || containsKey ( key) ) {
remove ( key) ;
return null ;
} else {
return null ;
}
} else {
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" ) ;
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" ) ;
String merge = map. merge ( "name" , "ll" , ( k, v) -> {
System . out. println ( k + "===" + v) ;
return k + v;
} ) ;
System . out. println ( "merge = " + merge) ;
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)
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)
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) -> {
if ( "hobby" . equals ( k) ) {
return v. toUpperCase ( ) ;
}
return v;
} ) ;
map. forEach ( ( k, v) -> System . out. println ( k + "=" + v) ) ;
}
当然还有其他的方法就不一一列举了,大家可以去试一下