Table of Contents
ToggleJava HashMap compute Method
計算一個新值並將它與指定的鍵相關聯,提供該鍵可能存在或不存在於 Map 中的關聯計算, Compute Java HashMap 介紹常見的 compute 、 computeIfAbsent 、 computeIfPresent 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- hashmap
| +- HashMapComputeTest.java
單元測試
HashMap Compute Java 提供該鍵存在或已存在於 Map 中的關聯計算。
compute
建立一個 HashMap ,增加三個元素,計算符合條件的新值。
@Test
public void compute() {
Integer expected = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
String key = "Grape";
map.compute(key, (k, v) -> v + 1);
System.out.println(map);
assertEquals(expected, map.get(key));
}
{Grape=1, Kiwifruit=2, Lemon=3}
{Grape=2, Kiwifruit=2, Lemon=3}
computeFunction
建立一個 HashMap ,增加三個元素,計算符合條件的新值。
@Test
public void computeFunction() {
Integer expected = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
String key = "Grape";
map.compute(key, (k, v) -> {
v += 1;
return v;
});
System.out.println(map);
assertEquals(expected, map.get(key));
}
{Grape=1, Kiwifruit=2, Lemon=3}
{Grape=2, Kiwifruit=2, Lemon=3}
computeNullPointerException
HashMap Compute Java 建立一個 HashMap ,內有三個元素,計算不存在的 Key 新值 ,會拋出 NullPointerException 例外。
@Test(expected = NullPointerException.class)
public void computeNullPointerException() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
String key = null;
map.compute(key, (k, v) -> v + 1);
System.out.println(map);
}
{Grape=1, Kiwifruit=2, Lemon=3}
computeCount
HashMap Compute Java 建立一個 HashMap ,計算 String 個數。
@Test
public void computeCount() {
String value = "Hello World, Java Learn";
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < value.length(); i++) {
String key = String.valueOf(value.charAt(i));
map.compute(key, (k, v) -> {
v = (v == null ? 1 : v + 1);
return v;
});
}
System.out.println(map);
}
{ =3, a=3, d=1, e=2, H=1, J=1, L=1, ,=1, l=3, n=1, o=2, r=2, v=1, W=1}
computeIfAbsent
HashMap Compute Java 建立一個 HashMap ,內有三個元素,計算不存在的 Key 新值。
@Test
public void computeIfAbsent() {
Integer expected = 4;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
String key = "Mango";
Integer result = map.computeIfAbsent(key, k -> 4);
System.out.println(result);
System.out.println(map);
assertEquals(expected, result);
key = "Grape";
result = map.computeIfAbsent(key, k -> 2);
System.out.println(result);
System.out.println(map);
assertEquals(1, result.intValue());
}
{Grape=1, Kiwifruit=2, Lemon=3}
4
{Grape=1, Mango=4, Kiwifruit=2, Lemon=3}
1
{Grape=1, Mango=4, Kiwifruit=2, Lemon=3}
computeIfPresent
HashMap Compute Java 建立一個 HashMap ,內有三個元素,計算已存在的 Key 新值。
@Test
public void computeIfPresent() {
Integer expected = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
String key = "Grape";
Integer result = map.computeIfPresent(key, (k, v) -> v + 1);
System.out.println(result);
System.out.println(map);
assertEquals(expected, result);
key = "Mango";
result = map.computeIfPresent(key, (k, v) -> 4);
System.out.println(result);
System.out.println(map);
assertNull(result);
}
{Grape=1, Kiwifruit=2, Lemon=3}
2
{Grape=2, Kiwifruit=2, Lemon=3}
null
{Grape=2, Kiwifruit=2, Lemon=3}
HashMapComputeTest.java
HashMap Compute Example 新增單元測試,驗證 Java HashMap Compute 是否符合預期。
package org.ruoxue.java_147.map.hashmap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class HashMapComputeTest {
public HashMapComputeTest() {
}
@Test
public void compute() {
Integer expected = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
String key = "Grape";
map.compute(key, (k, v) -> v + 1);
System.out.println(map);
assertEquals(expected, map.get(key));
}
@Test
public void computeFunction() {
Integer expected = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
String key = "Grape";
map.compute(key, (k, v) -> {
v += 1;
return v;
});
System.out.println(map);
assertEquals(expected, map.get(key));
}
@Test(expected = NullPointerException.class)
public void computeNullPointerException() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
String key = null;
map.compute(key, (k, v) -> v + 1);
System.out.println(map);
}
@Test
public void computeCount() {
String value = "Hello World, Java Learn";
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < value.length(); i++) {
String key = String.valueOf(value.charAt(i));
map.compute(key, (k, v) -> {
v = (v == null ? 1 : v + 1);
return v;
});
}
System.out.println(map);
}
@Test
public void computeIfAbsent() {
Integer expected = 4;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
String key = "Mango";
Integer result = map.computeIfAbsent(key, k -> 4);
System.out.println(result);
System.out.println(map);
assertEquals(expected, result);
key = "Grape";
result = map.computeIfAbsent(key, k -> 2);
System.out.println(result);
System.out.println(map);
assertEquals(1, result.intValue());
}
@Test
public void computeIfPresent() {
Integer expected = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
String key = "Grape";
Integer result = map.computeIfPresent(key, (k, v) -> v + 1);
System.out.println(result);
System.out.println(map);
assertEquals(expected, result);
key = "Mango";
result = map.computeIfPresent(key, (k, v) -> 4);
System.out.println(result);
System.out.println(map);
assertNull(result);
}
}
心得分享
Java HashMap Compute 提供了幾種 HashMap 常見方法的操作範例,在應用上相當廣泛,熟悉 HashMap Compute Example 這些方法的操作,舉例:compute 、 computeIfAbsent 、 computeIfPresent 等方法。