1. 程式人生 > >java8中Map的10個常用新方法

java8中Map的10個常用新方法

package com.jandmin.demo; import java.util.HashMap; import java.util.Map; import java.util.function.BiFunction; /** * @Description: java8中的map新方法 * @Author: JandMin */ public class MainTest { public static void main(String[] args) { //map 新特性 Map<Integer,String> map = new HashMap<Integer,String>(); for(int i=0; i<6; i++){ map.put(i,"val_"+i); } map.put(10,null); //1:遍歷 map.forEach((key,value) -> System.out

.println(key+":"+value)); //2:V getOrDefault(key,defaultValue):獲取key值,如果key不存在則用defaultValue System.out.println("3-->"+map.getOrDefault(3,"val_66"));//3-->val_3 System.out.println("10-->"+map.getOrDefault(10,"val_66"));//10-->null System.out.println("11-->"+map.getOrDefault(11,"val_66"));//11-->val_66 //3:V putIfAbsent(K key, V value):根據key匹配Node,如果匹配不到則增加key-value,返回null,如果匹配到Node,如果oldValue不等於null則不進行value覆蓋,返回oldValue System.out
.println(map.putIfAbsent(3,"val_66"));//val_3 System.out.println(map.putIfAbsent(10,"val_66"));//null System.out.println(map.putIfAbsent(11,"val_66"));//null System.out.println(map.get(3)+"--"+map.get(10)+"--"+map.get(11));//val_3--val_66--val_66 //4:boolean remove(Object key, Object value):根據key匹配node,如果value也相同則刪除 System.out
.println(map.size());//8 map.remove(10,"66"); map.remove(11,"val_66"); System.out.println(map.size());//7 System.out.println(map.toString());//{0=val_0, 1=val_1, 2=val_2, 3=val_3, 4=val_4, 5=val_5, 10=val_66} //5:boolean replace(K key, V oldValue, V newValue):根據key匹配node,如果value也相同則使用newValue覆蓋返回true,否則返回false map.put(11,null); map.replace(3,"3","33"); map.replace(10,"val_66","val_666666"); map.replace(11,null,"val_11"); map.replace(11,null,"val_11"); System.out.println(map.toString());//{0=val_0, 1=val_1, 2=val_2, 3=val_3, 4=val_4, 5=val_5, 10=val_666666, 11=val_11} /** 6: * void replaceAll(BiFunction function):呼叫此方法時重寫BiFunction的Object apply(Object o, Object o2)方法, * 其中o為key,o2為value,根據重寫方法邏輯進行重新賦值。 */ map.replaceAll((key,value) -> { if(key == 2){ return value+"222"; } return value; }); System.out.println(map.toString());//{0=val_0, 1=val_1, 2=val_2222, 3=val_3, 4=val_4, 5=val_5, 10=val_666666, 11=val_11} /** 7: * V compute(K key,BiFunction remappingFunction):根據key做匹配,根據BiFunction的apply返回做儲存的value。 * 匹配到Node做value替換,匹配不到新增node。apply的返回值如果為null則刪除該節點,否則即為要儲存的value。 */ System.out.println("---------------------- compute -----------------------"); System.out.println(map.compute(3,new BiFunction() { @Override public Object apply(Object key, Object value) { return key+":"+value; } }));//3:val_3 -》用返回值覆蓋原來的值,這裡用了java7的編碼方式,以下均採用java8的lanbda表示式 System.out.println(map.compute(10,(key,value) -> {return value.split("_")[1];}));//666666 -》用返回值覆蓋原來的值 System.out.println(map.compute(6,(key,value) -> null));//null -》返回值為null,則刪除該key值 System.out.println(map.toString());//{0=val_0, 1=val_1, 2=val_2, 3=3:val_3, 4=val_4, 5=val_5, 10=666666, 11=val_11} /** 8: * merge(K key, V value,BiFunctionsuper V, ? super V, ? extends V> remappingFunction): * 功能大部分與compute相同,不同之處在於BiFunction中apply的引數,入參為oldValue、value, * 呼叫merge時根據兩個value進行邏輯處理並返回value。 */ System.out.println(map.merge(3,"val_3",(value,newValue) -> newValue));//val_3 --》返回值覆蓋原來的value System.out.println(map.merge(10,"33334",(a,b) -> (Integer.valueOf(a)+Integer.valueOf(b))+""));//700000 System.out.println(map.merge(8,"88",(oldValue,newValue) -> oldValue+newValue));//88 -》key不存在則新增 System.out.println(map.merge(11,"11",(old,newValue) -> null));//null -》返回值為null,刪除該節點 System.out.println(map.toString());//{0=val_0, 1=val_1, 2=val_2, 3=val_3, 4=val_4, 5=val_5, 8=88, 10=700000} /** 9: * computeIfAbsent(K key,Functionsuper K, ? extends V> mappingFunction): * 根據key做匹配Node,(匹配不到則新建然後重排) * 如果Node有value,則直接返回oldValue, * 如果沒有value則根據Function介面的apply方法獲取value,返回value。 * Function介面的apply的入參為key,呼叫computeIfAbsent時重寫Function介面可以根據key進行邏輯處理, * apply的返回值即為要儲存的value。 */ System.out.println("----------------------computeIfAbsent------------------------"); map.put(8,null); System.out.println(map.toString());//{0=val_0, 1=val_1, 2=val_2, 3=val_3, 4=val_4, 5=val_5, 8=null, 10=700000} System.out.println(map.computeIfAbsent(0,key -> key+"000"));//val_0 -》key值存在,直接返回oldValue System.out.println(map.computeIfAbsent(7,key -> "value_"+key));//value_7 -》key匹配不到,直接新增,返回值為value System.out.println(map.computeIfAbsent(8,key -> "88"));//88 -》key匹配到了,value為null,返回值作為value System.out.println(map.toString());//{0=val_0, 1=val_1, 2=val_2, 3=val_3, 4=val_4, 5=val_5, 7=value_7, 8=88, 10=700000} /** 10: * V computeIfPresent(K key,BiFunction remappingFunction): * 根據key做匹配,如果匹配不上則返回null,匹配上根據BiFunction的apply方法獲取value,返回value。 * BiFunction介面的apply的入參為key、oldValue,呼叫computeIfPresent時重寫Function介面 * 可以根據key和oldValue進行邏輯處理,apply的返回值如果為null則刪除該節點,否則即為要儲存的value。 */ map.remove(7); map.remove(8); map.replace(10,null); map.remove(0,"val_0");//value匹配到了刪除 map.remove(1,"val_0");//value匹配失敗,不會刪除 System.out.println(map.toString());//{1=val_1, 2=val_2, 3=val_3, 4=val_4, 5=val_5, 10=null} System.out.println(map.computeIfPresent(3,(key,value) -> key+":"+value));//3:val_3 -》key存在,根據返回值修改value System.out.println(map.computeIfPresent(0,(key, value) -> "0000"));//null -》key不存在,返回null,不做任何操作 System.out.println(map.computeIfPresent(1,(key, value) -> null));//null -》key存在,根據返回值修改value System.out.println(map.computeIfPresent(10,(key,value) -> "val_10"));//null -》oldValue值為null,刪除節點 System.out.println(map.toString());//{2=val_2, 3=3:val_3, 4=val_4, 5=val_5, 10=null} /** 比較 * compute:根據key做匹配,key,value為引數,匹配到Node做value替換,匹配不到新增node。apply的返回值為null則刪除該節點。 * merge:oldValue,newValue作為為引數,其它功能於compute類似 * computeIfAbsent:根據key匹配,引數為key,存在且value不為null,不做修改,為null用返回值作為value,不存在則新增 * computeIfPresent:key,value作為引數,存在,原來的值為null不做操作,否則返回值作為新的value覆蓋原來;不存在,不做操作;返回值為null刪除該節點 * */ } } --------------------- 本文來自 JandMin 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/ItRuler/article/details/81002264?utm_source=copy