Table of Contents
ToggleJava ConcurrentHashMap Class
根據 Key 的 HashCode 提供細粒度鎖定,這表示僅鎖定正在修改的集合部分,而不是整個集合,對於並發操作具有高度可擴展性和高效性,但比其他同步機制需要額外的記憶體開銷,會使程式碼變得更加複雜, ConcurrentHashMap Class 介紹常見的 containsKey 、 stream 、 replaceAll 、 merge 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- concurrenthashmap
| +- ConcurrentHashMapClassTest.java
單元測試
ConcurrentHashMap Class 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();
}
}
containsKey
ConcurrentHashMap Class Java 建立一個 ConcurrentHashMap ,增加三個元素,檢查是否包含鍵值。
@Test
public void containsKey() {
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));
boolean containsKey = map.containsKey("Lemon");
System.out.println(containsKey);
assertTrue(containsKey);
}
true
containsValue
ConcurrentHashMap Class Java 建立一個 ConcurrentHashMap ,增加三個元素,檢查是否包含值。
@Test
public void containsValue() {
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));
boolean containsValue = map.containsValue(new Fruit("Grape", -1, 1));
System.out.println(containsValue);
assertTrue(containsValue);
}
true
stream
ConcurrentHashMap Class Java 建立一個 ConcurrentHashMap ,內有三個元素,使用串流,取得長度小於 6 的元素。
@Test
public void stream() {
int expectedSize = 2;
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));
Set<String> set = map.keySet().stream().filter(e -> e.length() < 6).collect(Collectors.toSet());
System.out.println(set);
assertEquals(expectedSize, set.size());
}
[Grape, Lemon]
parallelStream
ConcurrentHashMap Class Java 建立一個 ConcurrentHashMap ,內有三個元素,使用並行串流。
@Test
public void parallelStream() {
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));
map.keySet().parallelStream().forEach(System.out::println);
System.out.println("----------");
map.keySet().parallelStream().forEachOrdered(System.out::println);
}
Grape
Kiwifruit
Lemon
----------
Kiwifruit
Lemon
Grape
replace
Concurrent Hash Map Class in Java 建立一個 ConcurrentHashMap ,內有三個元素,取代指定 Key 元素的值。
@Test
public void replace() {
double expected = 10d;
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));
map.replace("Grape", new Fruit("Grape", 10, 1));
System.out.println(map);
assertEquals(expected, map.get("Grape").getQuantity(), 0);
}
{Grape={"name":"Grape","quantity":10.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}
replaceAll
Concurrent Hash Map Class in Java 建立一個 ConcurrentHashMap ,內有三個元素,取代所有元素的值。
@Test
public void replaceAll() {
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));
map.replaceAll((k, v) -> {
v.setQuantity(v.getQuantity() * 10);
return v;
});
System.out.println(map);
}
{Grape={"name":"Grape","quantity":-10.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":Infinity,"type":2}, Lemon={"name":"Lemon","quantity":10.0,"type":3}}
merge
Concurrent Hash Map Class in Java 建立一個 ConcurrentHashMap ,內有三個元素,指定 key 值,合併 value 舊值與新值。
@Test
public void merge() {
double expected = 9d;
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));
Fruit replaced = map.merge("Grape", new Fruit("Grape", 10, 1), (oldValue, newValue) -> {
newValue.setQuantity(oldValue.getQuantity() + newValue.getQuantity());
return newValue;
});
System.out.println(map);
assertEquals(expected, replaced.getQuantity(), 0);
replaced = map.merge("Papaya", new Fruit("Papaya", 4, 1), (oldValue, newValue) -> {
newValue.setQuantity(oldValue.getQuantity() + newValue.getQuantity());
return newValue;
});
System.out.println(map);
assertEquals(4d, replaced.getQuantity(), 0);
}
{Grape={"name":"Grape","quantity":9.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}
{Papaya={"name":"Papaya","quantity":4.0,"type":1}, Grape={"name":"Grape","quantity":9.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}
ConcurrentHashMapClassTest.java
Concurrent Hash Map Class in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.map.concurrenthashmap;
import static org.junit.Assert.*;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
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 ConcurrentHashMapClassTest {
@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 containsKey() {
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));
boolean containsKey = map.containsKey("Lemon");
System.out.println(containsKey);
assertTrue(containsKey);
}
@Test
public void containsValue() {
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));
boolean containsValue = map.containsValue(new Fruit("Grape", -1, 1));
System.out.println(containsValue);
assertTrue(containsValue);
}
@Test
public void stream() {
int expectedSize = 2;
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));
Set<String> set = map.keySet().stream().filter(e -> e.length() < 6).collect(Collectors.toSet());
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void parallelStream() {
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));
map.keySet().parallelStream().forEach(System.out::println);
System.out.println("----------");
map.keySet().parallelStream().forEachOrdered(System.out::println);
}
@Test
public void replace() {
double expected = 10d;
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));
map.replace("Grape", new Fruit("Grape", 10, 1));
System.out.println(map);
assertEquals(expected, map.get("Grape").getQuantity(), 0);
}
@Test
public void replaceAll() {
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));
map.replaceAll((k, v) -> {
v.setQuantity(v.getQuantity() * 10);
return v;
});
System.out.println(map);
}
@Test
public void merge() {
double expected = 9d;
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));
Fruit replaced = map.merge("Grape", new Fruit("Grape", 10, 1), (oldValue, newValue) -> {
newValue.setQuantity(oldValue.getQuantity() + newValue.getQuantity());
return newValue;
});
System.out.println(map);
assertEquals(expected, replaced.getQuantity(), 0);
replaced = map.merge("Papaya", new Fruit("Papaya", 4, 1), (oldValue, newValue) -> {
newValue.setQuantity(oldValue.getQuantity() + newValue.getQuantity());
return newValue;
});
System.out.println(map);
assertEquals(4d, replaced.getQuantity(), 0);
}
}
心得分享
Java ConcurrentHashMap Class Example 是 HashMap 的增強版本,實現執行緒安全,多個執行緒可以同時存取集合,而不會出現任何同步問題,可供多個線程同時使用,使其成為需要處理並發存取資料的理想選擇, Concurrent Hash Map Class in Java 提供 containsKey 、 stream 、 replaceAll 、 merge 等操作範例。