Table of Contents
ToggleJava ConcurrentHashSet Methods
在 java.util.concurrent 套件並沒有 ConcurrentHashSet 的類別,但從 JDK 8 開始,可以使用新加入的 keySet (defaultValue) 和 newKeySet 方法,建立一個由 ConcurrentHashMap 支援的 ConcurrentHashSet , ConcurrentHashSet Java Methods 介紹常見的 add 、 remove 、 clear 、 size 等方法,了解 Set 的不同操作和方法,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- set
| +- concurrenthashset
| +- ConcurrentHashSetMethodsTest.java
單元測試
ConcurrentHashSet Methods Java 提供新增、刪除等操作 Set 中的元素。
newKeySet
使用 ConcurrentHashMap newKeySet ,取得 Set ,增加三個元素。
@Test
public void newKeySet() {
int expectedSize = 3;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
System.out.println(set);
assertEquals(expectedSize, set.size());
}
[Pear, Longan, Tomato]
keySet
ConcurrentHashSet Methods Java 建立一個 ConcurrentHashMap ,使用 keySet ,取得 Set ,增加三個元素。
@Test
public void keySet() {
int expectedSize = 3;
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.put("Longan", 1);
map.put("Tomato", 2);
map.put("Pear", 3);
System.out.println(map);
Set<String> set = map.keySet();
System.out.println(set);
assertEquals(expectedSize, set.size());
}
{Pear=3, Longan=1, Tomato=2}
[Pear, Longan, Tomato]
keySetDefaultValue
ConcurrentHashSet Methods Java 建立一個 ConcurrentHashMap ,使用 keySet ,設定預設值,取得 Set ,增加三個元素。
@Test
public void keySetDefaultValue() {
int expectedSize = 3;
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
Set<String> set = map.keySet(0);
set.add("Longan");
set.add("Tomato");
set.add("Pear");
System.out.println(map);
System.out.println(set);
assertEquals(expectedSize, set.size());
}
{Pear=0, Longan=0, Tomato=0}
[Pear, Longan, Tomato]
Sets_newConcurrentHashSet
ConcurrentHashSet Methods Java 建立一個 ConcurrentHashMap ,使用 Guava Sets newConcurrentHashSet ,取得 Set ,增加三個元素。
@Test
public void Sets_newConcurrentHashSet() {
int expectedSize = 3;
Set<String> set = Sets.newConcurrentHashSet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
System.out.println(set);
assertEquals(expectedSize, set.size());
}
[Pear, Longan, Tomato]
addAll
ConcurrentHashSet Methods in Java 建立兩個 ConcurrentHashMap ,使用 newKeySet,取得 Set ,各有三個元素,合併成為一個 Set 。
@Test
public void addAll() {
int expectedSize = 6;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Set<String> set2 = ConcurrentHashMap.newKeySet();
set2.add("Grape");
set2.add("Lemon");
set2.add("Mango");
set.addAll(set2);
System.out.println(set);
assertEquals(expectedSize, set.size());
}
[Pear, Grape, Mango, Longan, Tomato, Lemon]
remove
ConcurrentHashSet Methods in Java 建立一個 ConcurrentHashMap ,使用 newKeySet ,取得 Set ,內有三個元素,刪除指定元素。
@Test
public void remove() {
int expectedSize = 2;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
set.remove("Longan");
System.out.println(set);
assertEquals(expectedSize, set.size());
}
[Pear, Tomato]
removeAll
ConcurrentHashSet Methods in Java 建立一個 ConcurrentHashMap ,使用 newKeySet ,取得 Set ,內有三個元素,刪除來自另一個 Set 中的元素。
@Test
public void removeAll() {
int expectedSize = 1;
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");
set2.add("Mango");
set.removeAll(set2);
System.out.println(set);
assertEquals(expectedSize, set.size());
}
[Pear]
clear
ConcurrentHashSet Methods in Java 建立一個 ConcurrentHashMap ,使用 newKeySet ,取得 Set ,內有三個元素,刪除所有元素。
@Test
public void clear() {
int expectedSize = 0;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
set.clear();
System.out.println(set);
assertEquals(expectedSize, set.size());
}
[]
size
ConcurrentHashSet Methods in Java 建立一個 ConcurrentHashMap ,使用 newKeySet ,取得 Set ,內有三個元素,取得集合大小。
@Test
public void size() {
int expectedSize = 3;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
System.out.println(set.size());
assertEquals(expectedSize, set.size());
}
3
isEmpty
ConcurrentHashSet Methods in Java 建立一個 ConcurrentHashMap ,使用 newKeySet ,取得 Set ,檢查是否為空 Set 。
@Test
public void isEmpty() {
Set<String> set = ConcurrentHashMap.newKeySet();
System.out.println(set.isEmpty());
assertTrue(set.isEmpty());
set.add("Longan");
set.add("Tomato");
set.add("Pear");
System.out.println(set.isEmpty());
assertFalse(set.isEmpty());
}
true
false
ConcurrentHashSetMethodsTest.java
ConcurrentHashSet Methods in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.set.concurrenthashset;
import static org.junit.Assert.*;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.Test;
import com.google.common.collect.Sets;
public class ConcurrentHashSetMethodsTest {
@Test
public void newKeySet() {
int expectedSize = 3;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void keySet() {
int expectedSize = 3;
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.put("Longan", 1);
map.put("Tomato", 2);
map.put("Pear", 3);
System.out.println(map);
Set<String> set = map.keySet();
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void keySetDefaultValue() {
int expectedSize = 3;
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
Set<String> set = map.keySet(0);
set.add("Longan");
set.add("Tomato");
set.add("Pear");
System.out.println(map);
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void Sets_newConcurrentHashSet() {
int expectedSize = 3;
Set<String> set = Sets.newConcurrentHashSet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void addAll() {
int expectedSize = 6;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
Set<String> set2 = ConcurrentHashMap.newKeySet();
set2.add("Grape");
set2.add("Lemon");
set2.add("Mango");
set.addAll(set2);
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void remove() {
int expectedSize = 2;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
set.remove("Longan");
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void removeAll() {
int expectedSize = 1;
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");
set2.add("Mango");
set.removeAll(set2);
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void clear() {
int expectedSize = 0;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
set.clear();
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void size() {
int expectedSize = 3;
Set<String> set = ConcurrentHashMap.newKeySet();
set.add("Longan");
set.add("Tomato");
set.add("Pear");
System.out.println(set.size());
assertEquals(expectedSize, set.size());
}
@Test
public void isEmpty() {
Set<String> set = ConcurrentHashMap.newKeySet();
System.out.println(set.isEmpty());
assertTrue(set.isEmpty());
set.add("Longan");
set.add("Tomato");
set.add("Pear");
System.out.println(set.isEmpty());
assertFalse(set.isEmpty());
}
}
心得分享
ConcurrentHashSet Functions in Java 調用 keySet (defaultValue) 和 newKeySet 的方法傳回 Set,可以新增元素以及執行其他集合運算,例如 add 、 remove 、 clear 、 size 等,熟悉 ConcurrentHashSet Methods in Java 這些方法的操作,可以快速撰寫程式,降低錯誤率。