Java 8之Map新增方法<轉>

在Java 8中的Map.Entry接口中增長了comparingByKeycomparingByValue方法,它們都返回Comparator<Map.Entry<K,V>>Comparator是一個函數接口,主要是方便Lambda表達式的使用。git

在Java 8中的Map接口增長了一些default方法,提高了對key, value操做的便利性。下面是基本數據的定義,經過這些數聽說明新增的一些方法。github

1
2
3
4
Map<Integer, String> map = new HashMap<>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");

getOrDefault 方法

若是指定的key存在,則返回該key對應的value,若是不存在,則返回指定的值。例子以下app

1
2
// key爲4不存在,輸出 d
System.out.println(map.getOrDefault(4, "d"));

forEach 方法

遍歷Map中的全部Entry, 對key, value進行處理, 接收參數 (K, V) -> void, 例子以下函數

1
2
// 輸出1a, 2b, 3c
map.forEach((key, value) -> System.out.println(key + value));

replaceAll 方法

替換Map中全部Entry的value值,這個值由舊的key和value計算得出,接收參數 (K, V) -> V, 相似以下代碼spa

1
2
for (Map.Entry<K, V> entry : map.entrySet())
entry.setValue(function.apply(entry.getKey(), entry.getValue()));

例如以下:code

1
2
3
map.replaceAll((key, value) -> (key + 1) + value);
// 輸出 12a 23b 34c
map.forEach((key, value) -> System.out.println(key + value));

putIfAbsent 方法

若是key關聯的value不存在,則關聯新的value值,返回key關聯的舊的值,相似以下代碼接口

1
2
3
4
5
V v = map.get(key);
if (v == null)
v = map.put(key, value);

return v;

示例代碼以下:rem

1
2
3
4
5
6
map.putIfAbsent(3, "d");
map.putIfAbsent(4, "d");
// 輸出 c
System.out.println(map.get(3));
// 輸出 d
System.out.println(map.get(4));

remove 方法

接收2個參數,key和value,若是key關聯的value值與指定的value值相等(equals),則刪除這個元素,相似代碼以下:get

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

示例代碼以下:string

1
2
3
4
5
6
7
map.remove(1, "b");
// 未刪除成功, 輸出 a
System.out.println(map.get(1));

map.remove(2, "b");
// 刪除成功,輸出 null
System.out.println(map.get(2));

replace(K key, V oldValue, V newValue) 方法

若是key關聯的值與指定的oldValue的值相等,則替換成新的newValue,相似代碼以下:

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
map.replace(3, "a", "z");
// 未替換成功,輸出 c
System.out.println(map.get(3));

map.replace(1, "a", "z");
// 替換成功, 輸出 z
System.out.println(map.get(1));

replace(K key, V value) 方法

若是map中存在key,則替換成value值,不然返回null, 相似代碼以下:

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

示例代碼以下:

1
2
3
4
5
6
7
8
9
// 輸出舊的值, a
System.out.println(map.replace(1, "aa"));
// 替換成功,輸出新的值, aa
System.out.println(map.get(1));

// 不存在key爲4, 輸出 null
System.out.println(map.replace(4, "d"));
// 不存在key爲4, 輸出 null
System.out.println(map.get(4));

computeIfAbsent 方法

若是指定的key不存在,則經過指定的K -> V計算出新的值設置爲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
map.computeIfAbsent(1, key -> key + " computed");
// 存在key爲1,則不進行計算,輸出值 a
System.out.println(map.get(1));

map.computeIfAbsent(4, key -> key + " computed");
// 不存在key爲4,則進行計算,輸出值 4 computed
System.out.println(map.get(4));

computeIfPresent 方法

若是指定的key存在,則根據舊的key和value計算新的值newValue, 若是newValue不爲null,則設置key新的值爲newValue, 若是newValue爲null, 則刪除該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
map.computeIfPresent(1, (key, value) -> (key + 1) + value);
// 存在key爲1, 則根據舊的key和value計算新的值,輸出 2a
System.out.println(map.get(1));

map.computeIfPresent(2, (key, value) -> null);
// 存在key爲2, 根據舊的key和value計算獲得null,刪除該值,輸出 null
System.out.println(map.get(2));

compute 方法

compute方法是computeIfAbsentcomputeIfPresent的綜合體。

merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) 方法

若是指定的key不存在,則設置指定的value值,不然根據key的舊的值oldvalue,value計算出新的值newValue, 若是newValue爲null, 則刪除該key,不然設置key的新值newValue。相似以下代碼:

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);

示例代碼以下:

1
2
3
4
5
6
// 存在key爲1, 輸出 a merge
System.out.println(map.merge(1, " merge", (oldValue, newValue) -> oldValue + newValue));
// 新值爲null,刪除key,輸出 null
System.out.println(map.merge(1, " merge", (oldValue, newValue) -> null));
// 輸出 " merge"
System.out.println(map.merge(4, " merge", (oldValue, newValue) -> oldValue + newValue));
相關文章
相關標籤/搜索