Java ConcurrentHashMap Methods - Java 147

Java ConcurrentHashMap Methods – Java 147

Java ConcurrentHashMap Methods

屬於 java.util.concurrent 套件,實作了 ConcurrentMap 以及 Serializable 介面,是 HashMap 的增強版本,實現執行緒安全,多個執行緒可以同時存取集合,而不會出現任何同步問題,介紹常見的 put 、 get 、 clear 、 size 等方法,將資料存儲在鍵、值對中,可以通過另一種類型的索引訪問,當插入重複鍵時,會替換相應鍵的元素, ConcurrentHashMap Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- map
       |                   +- concurrenthashmap
       |                       +- ConcurrentHashMapMethodsTest.java   

單元測試

ConcurrentHashMap Methods 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();
		}
	}

readWriteThrowException

ConcurrentHashMap Methods Java 建立一個 HashMap ,增加三個元素,多執行緒進行讀取元素時,同時寫入元素,會拋出例外。

	@Test
	public void readWriteThrowException() {
		try {
			int poolSize = 5;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			Map<String, Fruit> map = new HashMap<>();
			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));
			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					Iterator<String> it = map.keySet().iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			}

			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					map.put("Papaya", new Fruit("Papaya", 1, 1));
				});
			}
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
[12] Grape
[11] Grape
[12] Kiwifruit
[14] Grape
[13] Grape
[12] Lemon
[14] Kiwifruit
[14] Lemon
[15] Grape
[13] Kiwifruit
[11] Kiwifruit
Exception in thread "pool-1-thread-3" Exception in thread "pool-1-thread-1" Exception in thread "pool-1-thread-5" java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)
	at java.util.HashMap$KeyIterator.next(HashMap.java:1469)
	at org.ruoxue.java_147.map.concurrenthashmap.ConcurrentHashMapMethodsTest.lambda$0(ConcurrentHashMapMethodsTest.java:79)

readWrite

ConcurrentHashMap Methods Java 建立一個 ConcurrentHashMap ,增加三個元素,多執行緒進行讀寫元素時,能夠安全地遍歷集合。

	@Test
	public void readWrite() {
		try {
			int poolSize = 5;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			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));
			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					Iterator<String> it = map.keySet().iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			}

			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					map.put("Papaya", new Fruit("Papaya", 1, 1));
				});
			}
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
[12] Grape
[13] Grape
[12] Kiwifruit
[13] Kiwifruit
[12] Lemon
[13] Lemon
[15] Grape
[11] Grape
[14] Grape
[11] Kiwifruit
[15] Kiwifruit
[14] Kiwifruit
[11] Lemon
[15] Lemon
[14] Lemon

put

ConcurrentHashMap Methods Java 建立一個 ConcurrentHashMap ,增加三個元素。

	@Test
	public void put() {
		int expectedSize = 3;
		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);
		assertEquals(expectedSize, map.size());
	}
{Grape={"name":"Grape","quantity":-1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}

putIfAbsent

ConcurrentHashMap Methods Java 建立一個 ConcurrentHashMap ,當元素不存在時,新增元素。

	@Test
	public void putIfAbsent() {
		int expectedSize = 3;
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		Fruit put = map.putIfAbsent("Lemon", new Fruit("Lemon", 1, 3));
		System.out.println(put);
		assertNull(put);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}
null
{Grape={"name":"Grape","quantity":-1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}

get

ConcurrentHashMap Methods in Java 建立一個 ConcurrentHashMap ,內有三個元素,取得指定 Key 元素。

	@Test
	public void get() {
		double expected = Double.MAX_VALUE;
		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 value = map.get("Kiwifruit");
		System.out.println(value);
		assertEquals(expected, value.getQuantity(), 2);
	}
{"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}

getOrDefault

ConcurrentHashMap Methods in Java 建立一個 ConcurrentHashMap ,內有三個元素,取得指定 Key 元素,若不存在傳回預設值。

	@Test
	public void getOrDefault() {
		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 result = map.getOrDefault("", new Fruit("Empty", 0, 0));
		System.out.println(result);
		assertNotNull(result);
	}
{"name":"Empty","quantity":0.0,"type":0}

update

ConcurrentHashMap Methods in Java 建立一個 ConcurrentHashMap ,內有三個元素,更新指定 Key 元素。

	@Test
	public void update() {
		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));
		System.out.println(map);

		Fruit put = map.put("Grape", new Fruit("Grape", 10, 1));
		assertEquals(-1d, put.getQuantity(), 0);
		System.out.println(map);
		assertEquals(expected, map.get("Grape").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":10.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}

remove

ConcurrentHashMap Methods in Java 建立一個 ConcurrentHashMap ,內有三個元素,刪除指定 Key 元素。

	@Test
	public void remove() {
		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));
		map.remove("Grape");
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}
{Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}}

clear

ConcurrentHashMap Functions in Java 建立一個 ConcurrentHashMap ,內有三個元素,刪除所有元素。

	@Test
	public void clear() {
		int expectedSize = 0;
		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.clear();
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}
[]	

size

ConcurrentHashMap Functions in Java 建立一個 ConcurrentHashMap ,內有三個元素,取得長度, ConcurrentHashMap Java Methods 提供範例參考。

	@Test
	public void size() {
		int expectedSize = 3;
		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.size());
		assertEquals(expectedSize, map.size());
	}
3

putAll

ConcurrentHashMap Functions in Java 建立兩個 ConcurrentHashMap ,內各有三個元素,合併成為一個 Map , ConcurrentHashMap Java Methods 提供範例參考。

	@Test
	public void putAll() {
		int expectedSize = 6;
		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<String, Fruit> newMap = new ConcurrentHashMap<>();
		newMap.put("Apple", new Fruit("Apple", 4, 1));
		newMap.put("Banana", new Fruit("Banana", 5, 1));
		newMap.put("Cherry", new Fruit("Cherry", 6, 1));

		map.putAll(newMap);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}
{Apple={"name":"Apple","quantity":4.0,"type":1}, Cherry={"name":"Cherry","quantity":6.0,"type":1}, Grape={"name":"Grape","quantity":-1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.7976931348623157E308,"type":2}, Lemon={"name":"Lemon","quantity":1.0,"type":3}, Banana={"name":"Banana","quantity":5.0,"type":1}}

isEmpty

ConcurrentHashMap Functions in Java 建立一個 ConcurrentHashMap ,檢查是否為空 Map , ConcurrentHashMap Java Methods 提供範例參考。

	@Test
	public void isEmpty() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		System.out.println(map.isEmpty());
		assertTrue(map.isEmpty());
		map.put("Grape", new Fruit("Grape", -1, 1));
		System.out.println(map.isEmpty());
		assertFalse(map.isEmpty());
	}
true
false

ConcurrentHashMapMethodsTest.java

ConcurrentHashMap Methods in Java 新增單元測試,驗證 ConcurrentHashMap Functions in Java 是否符合預期。

package org.ruoxue.java_147.map.concurrenthashmap;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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 ConcurrentHashMapMethodsTest {

	@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 readWriteThrowException() {
		try {
			int poolSize = 5;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			Map<String, Fruit> map = new HashMap<>();
			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));
			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					Iterator<String> it = map.keySet().iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			}

			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					map.put("Papaya", new Fruit("Papaya", 1, 1));
				});
			}
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Test
	public void readWrite() {
		try {
			int poolSize = 5;
			ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
			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));
			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					Iterator<String> it = map.keySet().iterator();
					while (it.hasNext()) {
						System.out.println(String.format("[%d] %s", Thread.currentThread().getId(), it.next()));
					}
				});
			}

			for (int i = 0; i < poolSize; i++) {
				executorService.execute(() -> {
					map.put("Papaya", new Fruit("Papaya", 1, 1));
				});
			}
			Thread.sleep(2_000L);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Test
	public void put() {
		int expectedSize = 3;
		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);
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void putIfAbsent() {
		int expectedSize = 3;
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		map.put("Grape", new Fruit("Grape", -1, 1));
		map.put("Kiwifruit", new Fruit("Kiwifruit", Double.MAX_VALUE, 2));
		Fruit put = map.putIfAbsent("Lemon", new Fruit("Lemon", 1, 3));
		System.out.println(put);
		assertNull(put);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void get() {
		double expected = Double.MAX_VALUE;
		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 value = map.get("Kiwifruit");
		System.out.println(value);
		assertEquals(expected, value.getQuantity(), 2);
	}

	@Test
	public void getOrDefault() {
		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 result = map.getOrDefault("", new Fruit("Empty", 0, 0));
		System.out.println(result);
		assertNotNull(result);
	}

	@Test
	public void update() {
		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));
		System.out.println(map);

		Fruit put = map.put("Grape", new Fruit("Grape", 10, 1));
		assertEquals(-1d, put.getQuantity(), 0);
		System.out.println(map);
		assertEquals(expected, map.get("Grape").getQuantity(), 0);
	}

	@Test
	public void remove() {
		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));
		map.remove("Grape");
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void clear() {
		int expectedSize = 0;
		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.clear();
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void size() {
		int expectedSize = 3;
		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.size());
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void putAll() {
		int expectedSize = 6;
		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<String, Fruit> newMap = new ConcurrentHashMap<>();
		newMap.put("Apple", new Fruit("Apple", 4, 1));
		newMap.put("Banana", new Fruit("Banana", 5, 1));
		newMap.put("Cherry", new Fruit("Cherry", 6, 1));

		map.putAll(newMap);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void isEmpty() {
		Map<String, Fruit> map = new ConcurrentHashMap<>();
		System.out.println(map.isEmpty());
		assertTrue(map.isEmpty());
		map.put("Grape", new Fruit("Grape", -1, 1));
		System.out.println(map.isEmpty());
		assertFalse(map.isEmpty());
	}
}

心得分享

ConcurrentHashMap Functions in Java 提供細粒度鎖定,這表示僅鎖定正在修改的集合部分,而不是整個集合,對於並發操作具有高度可擴展性和高效性,熟悉 ConcurrentHashMap Methods in Java 這些方法的操作,提升開發效率,在應用上相當廣泛。

發佈留言