一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java8 Map中新增的方法使用總結

Java8 Map中新增的方法使用總結

2021-06-09 13:58隔葉黃鶯 Java教程

這篇文章主要介紹了Java8 Map中新增的方法使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

得益于 java 8 的 default 方法特性,java 8 對 map 增加了不少實用的默認方法,像 getordefault, foreach, replace, replaceall, putifabsent, remove(key, value), computeifpresent, computeifabsent, compute 和merge 方法。另外與 map 相關的 map.entry 也新加了多個版本的 comparingbykey 和 comparingbyvalue 方法。

為達到熟練運用上述除 getordefault 和 foreach 外的其他方法,有必要逐一體驗一番,如何調用,返回值以及調用后的效果如何。看看每個方法不至于 java 8 那么多年還總是  if(map.containskey(key))... 那樣的老套操作。

前注:map 新增方法對  present 的判斷是 map.containskey(key) && map.get(key) != null,簡單就是  map.get(key) != null,也就是即使 key 存在,但對應的值為 null 的話也視為 absent。absent 就是 map.get(key) == null。

不同 map 實現對 key/value 是否能為 null 有不同的約束, hashmap, linkedhashmap, key 和 value 都可以為 null 值,treemap 的 key 為不能為 null, 但 value 可以為 null, 而 hashtable, concurrentmap 則 key 和 value 都不同為 null。一句話 absent/present 的判斷是 map.get(key) 是否為 null。

方法介紹的順序是它們相對于本人的生疏程度而定的。每個方法介紹主要分兩部分,參考實現代碼與示例代碼執行效果。參考實現代碼摘自 jdk 官方的 map javadoc。

putifabsent 方法

方法原型 v putifabsent(k key, v value) ,  如果 key 不存在或相關聯的值為 null, 則設置新的 key/value 值。

參考實現:

?
1
2
3
4
5
v v = get(key);
if (v == null) {
 v = put(key, value);
}
return v;

如果原 map 中對應 key 的值為為 null 返回舊值,或者返回新的 value 值

示例及效果:

?
1
2
3
4
5
6
7
string ret;
map<string, string> map = new hashmap<>();
ret = map.putifabsent("a", "aaa"); //ret 為"aaa", map 為 {"a":"aaa"}
ret = map.putifabsent("a", "bbb"); //ret 為 "aaa", map 還是 {"a":"aaa"}
 
map.put("b", null);
ret = map.putifabsent("b", "bbb"); //ret 為 "bbb", map 為 {"a":"aaa","b":"bbb"}

computeifpresent 方法

方法原型 v computeifpresent(k key, bifunction<? super k, ? super v, ? extends v> remappingfunction),如果指定的 key 存在并且相關聯的 value 不為 null 時,根據舊的 key 和 value 計算 newvalue 替換舊值,newvalue 為 null 則從 map 中刪除該 key; key 不存在或相應的值為 null 時則什么也不做,方法的返回值為最終的 map.get(key)。

參考實現:

?
1
2
3
4
5
6
7
8
if (map.get(key) != null) {
 v oldvalue = map.get(key);
 v newvalue = remappingfunction.apply(key, oldvalue);
 if (newvalue != null)
  map.put(key, newvalue);
 else
  map.remove(key);
}

示例及效果:

?
1
2
3
4
5
6
7
8
string ret;
map<string, string> map = new hashmap<>();
ret = map.computeifpresent("a", (key, value) -> key + value); //ret null, map 為 {}
map.put("a", null); //map 為 ["a":null]
ret = map.computeifpresent("a", (key, value) -> key + value); //ret null, map 為 {"a":null}
map.put("a", "+aaa");
ret = map.computeifpresent("a", (key, value) -> key + value); //ret "a+aaa", map 為 {"a":"a+aaa"}
ret = map.computeifpresent("a", (key, value) -> null); //ret 為 null, map 為 {},計算出的 null 把 key 刪除了

計算出的值為 null 時直接刪除 key 而不是設置對應 key 的值為 null, 這能照顧到值不能為 null 的 map 實現,如 hashtable 和 concurrentmap。

computeifabsent 方法

方法原型 v computeifabsent(k key, function<? super <, ? extends v> mappingfunction), 與上一個方法相反,如果指定的 key 不存在或相關的 value 為 null 時,設置 key 與關聯一個計算出的非 null 值,計算出的值為 null 的話什么也不做(不會去刪除相應的  key)。如果 key 存在并且對應 value 為 null 的話什么也不做。同樣,方法的返回值也是最終的 map.get(key)。

參考實現:

?
1
2
3
4
5
if (map.get(key) == null) {
 v newvalue = mappingfunction.apply(key);
 if (newvalue != null)
  map.put(key, newvalue);
}

示例及效果:

?
1
2
3
4
5
6
7
string ret;
map<string, string> map = new hashmap<>();
ret = map.computeifabsent("a", key -> key + "123"); //ret "a123", map 為 {"a":"a123"}
ret = map.computeifabsent("a", key -> key + "456"); //ret "a123", map 為 {"a":"a123"}
map.put("a", null);
ret = map.computeifabsent("a", key -> key + "456"); //ret "a456", map 為 {"a":"a456"}
ret = map.computeifabsent("a", key -> null); //ret 為 "a456", map 為 {"a":"a456"}

replace(k key, v value) 方法

只要 key 存在,不管對應值是否為  null,則用傳入的 value 替代原來的值。即使傳入的 value 是 null 也會用來替代原來的值,而不是刪除,注意這對于 value 不能為  null 值的  map  實現將會造成 nullpointerexception。key 不存在不會修改 map 的內容,返回值總是原始的 map.get(key) 值。

參考實現:

?
1
2
3
4
if (map.containskey(key)) {
 return map.put(key, value);
} else
 return null;

示例及效果:

?
1
2
3
4
5
6
7
string ret;
map<string, string> map = new hashmap<>();
ret = map.replace("a", "abc"); //ret 為 null,map 為 {}
map.put("a", "ddd");
ret = map.replace("a", "abc"); //ret 為 "ddd", map 為 {"a":"abc"}
ret = map.replace("a", null); //ret 為 "abc", map 為 {"a":null}
ret = map.replace("a", "ddd"); //ret 為 null, map 為 {"a":"ddd"}

replace(k key, v oldvalue, v newvalue)

當且僅當 key 存在,并且對應值與 oldvalue 不相等,才用 newvalue 作為 key 的新相關聯值,返回值為是否進行了替換。

參考實現:

?
1
2
3
4
5
if (map.containskey(key) && objects.equals(map.get(key), value)) {
 map.put(key, newvalue);
 return true;
} else
 return false;

示例及效果:

?
1
2
3
4
5
6
7
boolean ret;
map<string, string> map = new hashmap<>() ;
ret = map.replace("a", null, "aaa"); //ret 為 false, map 為 {}
map.put("a", null);
ret = map.replace("a", null, "aaa"); //ret 為 true, map 為 {"a":"aaa"}
ret = map.replace("a", "aaa", null); //ret 為 true, map 為 {"a":null}
ret = map.replace("a", "aaa", "bbb");//ret 為 false, map 為 {"a":null}

replaceall 方法

方法原型 void replaceall(bifunction<? super k, ? super v, ? extends v> function)。它更像一個傳統函數型語言的 map 函數,即對于 map 中的每一個元素應用函數 function, 輸入為 key 和  value。

參考實現:

?
1
2
for (map.entry<k, v> entry : map.entryset())
 entry.setvalue(function.apply(entry.getkey(), entry.getvalue()));

示例及效果:

?
1
2
3
4
map<string, string> map = new hashmap<>() ;
map.put("a", "aaa");
map.put("b", "bbb"); //map 為 {"a":"aaa","b":"bbb"}
map.replaceall((key, value) -> key + "-" + value); //map 為 {"a":"a-aaa","b":"b-bbb"}

remove(key, value)

這個也不用多說,key 與 value 都匹配時才刪除。

參考實現:

?
1
2
3
4
5
if (map.containskey(key) && objects.equals(map.get(key), value)) {
 map.remove(key);
 return true;
} else
 return false;

compute 方法

方法原型 v compute(k key, bifunction<? super k, ? super v, ? extends v> remappingfunction), 它是 computeifabsent 與 computeifpresent  的結合體。也就是既不管 key 存不存在,也不管 key 對應的值是否為 null, compute 死活都要設置與 key 相關聯的值,或者計算出的值為 null 時刪除相應的 key, 返回值為最終的 map.get(key)。

參考實現:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
v oldvalue = map.get(key);
v newvalue = remappingfunction.apply(key, oldvalue);
if (oldvalue != null ) {
if (newvalue != null)
 map.put(key, newvalue);
else
 map.remove(key);
} else {
if (newvalue != null)
 map.put(key, newvalue);
else
 return null;
}

示例及效果:

?
1
2
3
4
5
string ret;
map<string, string> map = new hashmap<>() ;
ret = map.compute("a", (key, value) -> "a" + value); //ret="anull", map={"a":"anull"}
ret = map.compute("a", (key, value) -> "a" + value); //ret="aanull", map={"a":"aanull"}
ret = map.compute("a", (key, value) -> null); //ret=null, map={}

merge 方法

方法原型 v merge(k key, v value, bifunction<? super v, ? super v, ? extends v> remappingfucntion),這是至今來說比較神秘的一個方法,尚未使用到它。如果指定的 key 不存在,或相應的值為 null 時,則設置  value 為相關聯的值。否則根據 key 對應的舊值和 value 計算出新的值 newvalue,newvalue 為 null 時,刪除該key, 否則設置 key 對應的值為  newvalue。方法的返回值也是最終的  map.get(key) 值。

參考實現:

?
1
2
3
4
5
6
7
v oldvalue = map.get(key);
 v newvalue = (oldvalue == null) ? value :
    remappingfunction.apply(oldvalue, value);
 if (newvalue == null)
  map.remove(key);
 else
  map.put(key, newvalue);

注意 value 不能為 null 值

示例及效果:

?
1
2
3
4
5
6
7
8
9
10
string ret;
map<string, string> map = new hashmap<>() ;
ret = map.merge("a", "aa", (oldvalue, value) -> oldvalue + "-" + value); //ret="aa", map={"a":"aa"}
ret = map.merge("a", "bb", (oldvalue, value) -> oldvalue + "-" + value); //ret="aa-bb", map={"a":"aa-bb"}
ret = map.merge("a", "bb", (oldvalue, value) -> null); //ret=null, map={}
map.put("a", null);
ret = map.merge("a", "aa", (oldvalue, value) -> oldvalue + "-" + value); //ret="aa", map={"a":"aa"}
map.put("a", null);
ret = map.merge("a", "bb", (oldvalue, value) -> null); //ret="bb", map={"a":"bb"}
ret = map.merge("a", null, (oldvalue, value) -> oldvalue + "-" + value); //nullpointerexception, value 不能為 null

map.entry comparingbykey 和  comparingbyvalue 方法

另外介紹一下 map.entry 新加的兩個排序方法,它們分別有無參與帶 comparator 參數可嵌套使用的兩個版本。comparingbykey(), comparingbykey(comparator<? super k> cmp), comparingbyvalue() 和 comparingbyvalue(comparator<? super v> cmp)。

示例代碼如下:

?
1
2
3
4
map.entryset().stream().sorted(map.entry.comparingbykey()).collect(collectors.tolist());
map.entryset().stream().sorted(map.entry.comparingbykey(string::compareto)).collect(collectors.tolist());
map.entryset().stream().sorted(map.entry.comparingbyvalue()).collect(collectors.tolist());
map.entryset().stream().sorted(map.entry.comparingbyvalue(string::compareto)).collect(collectors.tolist());

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://yanbin.blog/java-8-map-new-added-methods/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美一区二区三区免费不卡 | 国产videos hd | 第一福利在线观看永久视频 | 亚洲精品乱码蜜桃久久久 | 91香蕉视频在线播放 | 成人看片免费无限观看视频 | 精品性影院一区二区三区内射 | 天堂a视频| 97久久久亚洲综合久久88 | 天天操天天射天天爽 | 国产精品热久久毛片 | 亚州人成网在线播放 | 全肉一女n男np高h乳 | 日本欧美不卡一区二区三区在线 | 美女张开下身让男人桶 | 好男人资源大全免费观看 | 日日艹 | 精品综合久久久久久8888 | 国产精品色片 | 亚洲大片在线观看 | 嫩草成人影院 | 免费精品视频在线 | 91亚洲在线| 亚州精品视频 | 9丨精品国产高清自在线看 9久热这里只有精品免费 | 天天快乐在线观看 | 国产日韩高清一区二区三区 | 天天操免费视频 | 免费一看一级欧美 | 99久久精品国语对白 | 视频久久精品 | fuqer日本 | 日本xxxxxxxxx59| 美女撒尿无遮挡免费中国 | 我们中文在线观看免费完整版 | 性派对videofreeparty | 亚洲成色| 国产精品亚洲片在线va | 亚洲精品久久久成人 | 成人私人影院www片免费高清 | 青青网在线视频 |