Table of Contents
ToggleJava ConcurrentHashSet Class
從 JDK 8 開始,可以使用新加入的 keySet (defaultValue) 和 newKeySet 方法,建立一個由 ConcurrentHashMap 支援的 ConcurrentHashSet ,ConcurrentHashSet Class 介紹常見的 contains 、 stream 、 retainAll 等方法,了解 Set 的不同操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- set
| +- concurrenthashset
| +- ConcurrentHashSetClassTest.java
單元測試
ConcurrentHashSet Class Java 提供檢查包含、串流操作列表中的元素。
contains
使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,檢查包含指定的元素。
@Test
public void contains() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
boolean contains = set.contains("Longan");
System.out.println(contains);
assertTrue(contains);
contains = set.contains("Grape");
System.out.println(contains);
assertFalse(contains);
}
true
false
containsAll
ConcurrentHashSet Class Java 使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,檢查是否包含值。
@Test
public void containsAll() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Set<String> set2 = ConcurrentHashMap.newKeySet();
set2.add("Longan");
set2.add("Tomato");
boolean contains = set.containsAll(set2);
System.out.println(contains);
assertTrue(contains);
contains = set2.containsAll(set);
System.out.println(contains);
assertFalse(contains);
}
true
false
stream
ConcurrentHashSet Class Java 使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,執行串流,取得長度小於 6 的元素。
@Test
public void stream() {
int expectedSize = 1;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
set = set.stream().filter(e -> e.length() < 6).collect(Collectors.toSet());
System.out.println(set);
assertEquals(expectedSize, set.size());
}
[Pear]
parallelStream
ConcurrentHashSet Class Java 使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,並行執行串流。
@Test
public void parallelStream() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
set.parallelStream().forEach(System.out::println);
System.out.println("----------");
set.parallelStream().forEachOrdered(System.out::println);
}
Pear
Longan
Tomato
----------
Longan
Tomato
Pear
retainAll
使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,保留指定集合中包含的所有元素。
@Test
public void retainAll() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Set<String> set2 = ConcurrentHashMap.newKeySet();
set2.add("Longan");
set2.add("Lemon");
set2.add("Mango");
boolean result = set.retainAll(set2);
System.out.println(result);
System.out.println(set);
assertTrue(result);
result = set.retainAll(set2);
System.out.println(result);
System.out.println(set);
assertFalse(result);
}
true
[Longan]
false
[Longan]
ConcurrentHashSetClassTest.java
Concurrent Hash Set Class in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.set.concurrenthashset;
import static org.junit.Assert.*;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.junit.Test;
public class ConcurrentHashSetClassTest {
@Test
public void contains() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
boolean contains = set.contains("Longan");
System.out.println(contains);
assertTrue(contains);
contains = set.contains("Grape");
System.out.println(contains);
assertFalse(contains);
}
@Test
public void containsAll() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Set<String> set2 = ConcurrentHashMap.newKeySet();
set2.add("Longan");
set2.add("Tomato");
boolean contains = set.containsAll(set2);
System.out.println(contains);
assertTrue(contains);
contains = set2.containsAll(set);
System.out.println(contains);
assertFalse(contains);
}
@Test
public void stream() {
int expectedSize = 1;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
set = set.stream().filter(e -> e.length() < 6).collect(Collectors.toSet());
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void parallelStream() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
set.parallelStream().forEach(System.out::println);
System.out.println("----------");
set.parallelStream().forEachOrdered(System.out::println);
}
@Test
public void retainAll() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Set<String> set2 = ConcurrentHashMap.newKeySet();
set2.add("Longan");
set2.add("Lemon");
set2.add("Mango");
boolean result = set.retainAll(set2);
System.out.println(result);
System.out.println(set);
assertTrue(result);
result = set.retainAll(set2);
System.out.println(result);
System.out.println(set);
assertFalse(result);
}
}
心得分享
Java ConcurrentHashSet Class Example 調用 keySet (defaultValue) 和 newKeySet 的方法傳回 Set ,提供了幾種常見方法的操作範例, Concurrent Hash Set Class in Java 在應用上相當廣泛,熟悉這些方法的操作,能夠提高開發效率,節省維護上的成本。