Table of Contents
ToggleConcurrentHashSet in Java with Examples
調用 keySet (defaultValue) 和 newKeySet 的方法傳回 Set,可以新增元素以及執行其他集合運算, ConcurrentHashSet in Java 介紹常見的 forEach 、 iterator 、 spliterator 、 trySplit 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- set
| +- concurrenthashset
| +- ConcurrentHashSetWithExamplesTest.java
單元測試
ConcurrentHashSet Java 提供循環訪問、轉成陣列等操作 Set 中的元素。
forEach
使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,迴圈取得元素。
@Test
public void forEach() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
set.forEach(e -> System.out.println(e));
}
Pear
Longan
Tomato
forEachRemaining
ConcurrentHashSet Java 使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,迴圈取得剩餘元素。
@Test
public void forEachRemaining() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Iterator<String> it = set.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println(it.next());
if (i == 1) {
break;
}
i++;
}
System.out.println("----------");
it.forEachRemaining(e -> {
System.out.println(e);
});
}
Pear
Longan
----------
Tomato
iterator
ConcurrentHashSet Java 使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,迴圈取得元素。
@Test
public void iterator() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Iterator<String> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
Pear
Longan
Tomato
spliterator
ConcurrentHashSet Java 使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,使用 spliterator 取得元素,等同於 iterator.hasNext 和 iterator.next。
@Test
public void spliterator() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Spliterator<String> sit = set.spliterator();
sit.tryAdvance(e -> System.out.println(e));
System.out.println("----------");
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
sit = set.spliterator();
while (sit.tryAdvance(e -> System.out.println(e))) {
}
}
Pear
----------
Longan
Tomato
----------
Pear
Longan
Tomato
trySplit
Java ConcurrentHashSet 使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,使用 spliterator 取得元素,trySplit 將目前的拆分器分為兩個新的拆分器並行處理。
@Test
public void trySplit() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Spliterator<String> sit = set.spliterator();
Spliterator<String> sit2 = sit.trySplit();
System.out.println(sit.getExactSizeIfKnown());
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
System.out.println(sit2.getExactSizeIfKnown());
sit2.forEachRemaining(e -> System.out.println(e));
}
-1
Longan
Tomato
----------
-1
Pear
toArray
Java ConcurrentHashSet 使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,轉換成 String 陣列。
@Test
public void toArray() {
int expectedSize = 3;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
String[] array = new String[set.size()];
set.toArray(array);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
Pear
Longan
Tomato
streamToArray
Java ConcurrentHashSet 使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素,使用 Stream ,轉換成 String 陣列。
@Test
public void streamToArray() {
int expectedSize = 3;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
String[] array = set.stream().toArray(String[]::new);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
Pear
Longan
Tomato
ConcurrentHashSetWithExamplesTest.java
Java ConcurrentHashSet 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.set.concurrenthashset;
import static org.junit.Assert.*;
import java.util.Iterator;
import java.util.Set;
import java.util.Spliterator;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.Test;
public class ConcurrentHashSetWithExamplesTest {
@Test
public void forEach() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
set.forEach(e -> System.out.println(e));
}
@Test
public void forEachRemaining() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Iterator<String> it = set.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println(it.next());
if (i == 1) {
break;
}
i++;
}
System.out.println("----------");
it.forEachRemaining(e -> {
System.out.println(e);
});
}
@Test
public void iterator() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Iterator<String> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
@Test
public void spliterator() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Spliterator<String> sit = set.spliterator();
sit.tryAdvance(e -> System.out.println(e));
System.out.println("----------");
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
sit = set.spliterator();
while (sit.tryAdvance(e -> System.out.println(e))) {
}
}
@Test
public void trySplit() {
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Spliterator<String> sit = set.spliterator();
Spliterator<String> sit2 = sit.trySplit();
System.out.println(sit.getExactSizeIfKnown());
sit.forEachRemaining(e -> System.out.println(e));
System.out.println("----------");
System.out.println(sit2.getExactSizeIfKnown());
sit2.forEachRemaining(e -> System.out.println(e));
}
@Test
public void toArray() {
int expectedSize = 3;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
String[] array = new String[set.size()];
set.toArray(array);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
@Test
public void streamToArray() {
int expectedSize = 3;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
String[] array = set.stream().toArray(String[]::new);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
}
心得分享
Java ConcurrentHashSet Example 從 JDK 8 開始,可以使用新加入的 keySet (defaultValue) 和 newKeySet 方法,建立一個由 ConcurrentHashMap 支援的 ConcurrentHashSet ,提供了幾種常見方法的操作範例,在應用上相當廣泛,熟悉 Java ConcurrentHashSet 這些方法的操作,可以快速撰寫程式,降低錯誤率,再輔以單元測試驗證,建置高效穩定的服務或系統。