Table of Contents
ToggleJava ConcurrentHashMap compute Method
使用所提供的映射方法 BiFunction 為指定鍵計算一個新值,該鍵可能存在或不存在於 Map 中, Compute Java ConcurrentHashMap 介紹常見的 compute 、 computeIfAbsent 、 computeIfPresent 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
}
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
}
default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- concurrenthashmap
| +- ConcurrentHashMapComputeTest.java
單元測試
ConcurrentHashMap Compute Java 提供該鍵存在或已存在於 Map 中的關聯計算。
Fruit
建立 Fruit 類別,覆寫 equals 、 hashCode ,定義屬性和方法,用來建立一個物件。
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
public boolean equals(Object object) {
if (!(object instanceof Fruit)) {
return false;
}
if (this == object) {
return true;
}
Fruit other = (Fruit) object;
return new EqualsBuilder().append(getName(), other.getName()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getName()).toHashCode();
}
}
compute
ConcurrentHashMap Compute Java 建立一個 ConcurrentHashMap ,增加三個元素,計算符合條件的新值。
@Test
public void compute() {
double expected = 0d;
Map<String, Fruit> map = new ConcurrentHashMap<>();
map.put("Grape", new Fruit("Grape", -1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
map.put("Lemon", new Fruit("Lemon", 1, 3));
System.out.println(map);
String key = "Grape";
map.compute(key, (k, v) -> {
v.setQuantity(v.getQuantity() + 1);
return v;
});
System.out.println(map);
assertEquals(expected, map.get(key).getQuantity(), 0);
}
{Grape={"name":"Grape","quantity":-1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}
{Grape={"name":"Grape","quantity":0.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}
computeCount
ConcurrentHashMap Compute Java 建立一個 ConcurrentHashMap ,計算 String 個數。
@Test
public void computeCount() {
String value = "Hello World, Java Learn";
Map<String, Integer> map = new ConcurrentHashMap<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=3, ,=1, L=1, n=1, o=2, r=2, v=1, W=1}
computeIfAbsent
ConcurrentHashMap Compute Java 建立一個 ConcurrentHashMap ,內有三個元素,計算不存在的 Key 新值。
@Test
public void computeIfAbsent() {
double expected = 4d;
Map<String, Fruit> map = new ConcurrentHashMap<>();
map.put("Grape", new Fruit("Grape", -1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
map.put("Lemon", new Fruit("Lemon", 1, 3));
System.out.println(map);
String key = "Mango";
Fruit result = map.computeIfAbsent(key, k -> new Fruit("Mango", 4, 1));
System.out.println(map);
assertEquals(expected, result.getQuantity(), 0);
key = "Grape";
result = map.computeIfAbsent(key, k -> new Fruit("Grape", 2, 1));
assertEquals(-1d, result.getQuantity(), 0);
}
{Grape={"name":"Grape","quantity":-1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}
{Grape={"name":"Grape","quantity":-1.0,"type":1}, Mango={"name":"Mango","quantity":4.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}
computeIfPresent
ConcurrentHashMap Compute Java 建立一個 ConcurrentHashMap ,內有三個元素,計算已存在的 Key 新值。
@Test
public void computeIfPresent() {
double expected = 0d;
Map<String, Fruit> map = new ConcurrentHashMap<>();
map.put("Grape", new Fruit("Grape", -1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
map.put("Lemon", new Fruit("Lemon", 1, 3));
System.out.println(map);
String key = "Grape";
Fruit result = map.computeIfPresent(key, (k, v) -> {
v.setQuantity(v.getQuantity() + 1);
return v;
});
System.out.println(map);
assertEquals(expected, result.getQuantity(), 0);
key = "Mango";
result = map.computeIfPresent(key, (k, v) -> {
v.setQuantity(4);
return v;
});
assertNull(result);
}
{Grape={"name":"Grape","quantity":-1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}
{Grape={"name":"Grape","quantity":0.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}
ConcurrentHashMapComputeTest.java
ConcurrentHashMap Compute Example 新增單元測試,驗證 Java ConcurrentHashMap Compute 是否符合預期。
package org.ruoxue.java_147.map.concurrenthashmap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class ConcurrentHashMapComputeTest {
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
public boolean equals(Object object) {
if (!(object instanceof Fruit)) {
return false;
}
if (this == object) {
return true;
}
Fruit other = (Fruit) object;
return new EqualsBuilder().append(getName(), other.getName()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getName()).toHashCode();
}
}
@Test
public void compute() {
double expected = 0d;
Map<String, Fruit> map = new ConcurrentHashMap<>();
map.put("Grape", new Fruit("Grape", -1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
map.put("Lemon", new Fruit("Lemon", 1, 3));
System.out.println(map);
String key = "Grape";
map.compute(key, (k, v) -> {
v.setQuantity(v.getQuantity() + 1);
return v;
});
System.out.println(map);
assertEquals(expected, map.get(key).getQuantity(), 0);
}
@Test
public void computeCount() {
String value = "Hello World, Java Learn";
Map<String, Integer> map = new ConcurrentHashMap<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() {
double expected = 4d;
Map<String, Fruit> map = new ConcurrentHashMap<>();
map.put("Grape", new Fruit("Grape", -1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
map.put("Lemon", new Fruit("Lemon", 1, 3));
System.out.println(map);
String key = "Mango";
Fruit result = map.computeIfAbsent(key, k -> new Fruit("Mango", 4, 1));
System.out.println(map);
assertEquals(expected, result.getQuantity(), 0);
key = "Grape";
result = map.computeIfAbsent(key, k -> new Fruit("Grape", 2, 1));
assertEquals(-1d, result.getQuantity(), 0);
}
@Test
public void computeIfPresent() {
double expected = 0d;
Map<String, Fruit> map = new ConcurrentHashMap<>();
map.put("Grape", new Fruit("Grape", -1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
map.put("Lemon", new Fruit("Lemon", 1, 3));
System.out.println(map);
String key = "Grape";
Fruit result = map.computeIfPresent(key, (k, v) -> {
v.setQuantity(v.getQuantity() + 1);
return v;
});
System.out.println(map);
assertEquals(expected, result.getQuantity(), 0);
key = "Mango";
result = map.computeIfPresent(key, (k, v) -> {
v.setQuantity(4);
return v;
});
assertNull(result);
}
}
心得分享
Java ConcurrentHashMap Compute 提供了幾種 ConcurrentHashMap 常見方法的操作範例,在應用上相當廣泛,熟悉 ConcurrentHashMap Compute Example 這些方法的操作,例如: compute 、 computeIfAbsent 、 computeIfPresent 等方法,可以讓程式碼更加地簡潔及容易維護。