Java 8之Map新增方法<轉>


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

在Java 8中的Map接口增加了一些default方法,提升了對key, value操作的便利性。下面是基本數據的定義,通過這些數據說明新增的一些方法。

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,如果不存在,則返回指定的值。例子如下

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, 類似如下代碼

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

例如如下:

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;

示例代碼如下:

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),則刪除這個元素,類似代碼如下:

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

示例代碼如下:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM