Java : ConcurrentHashMap.KeySetView (並列処理用セット) - API使用例
ConcurrentHashMap.KeySetView (Java SE 20 & JDK 20) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
ConcurrentHashMap.KeySetView クラスは、反復処理 (例えば for-eachループ文) 中に add や remove などによる変更を許容する Set の実装です。
非チェック例外の ConcurrentModificationException が発生しないことが保証されています。
汎用コレクションの HashSet に相当します。
内部的には ConcurrentHashMap が使われており、ConcurrentHashMap.newKeySet メソッドでインスタンスを生成します。
void test(Set<String> set) throws InterruptedException {
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("e");
try (final var executor = Executors.newSingleThreadScheduledExecutor()) {
executor.schedule(() -> {
System.out.println("-- add value! --");
set.add("X");
}, 5, TimeUnit.SECONDS);
try {
for (final var value : set) {
System.out.println("value = " + value);
TimeUnit.SECONDS.sleep(2);
}
} catch (ConcurrentModificationException e) {
System.out.println("ConcurrentModificationException!");
}
}
System.out.println("-- end --");
System.out.println("set = " + set);
}
test(new HashSet<>());
// 結果
// ↓
//value = a
//value = b
//value = c
//-- add value! --
//ConcurrentModificationException!
//-- end --
//set = [a, b, c, d, e, X]
test(ConcurrentHashMap.newKeySet());
// 結果
// ↓
//value = a
//value = b
//value = c
//-- add value! --
//value = d
//value = e
//value = X
//-- end --
//set = [a, b, c, d, e, X]
メソッド
boolean add (K e)
final var set = ConcurrentHashMap.<String>newKeySet();
System.out.println(set); // []
System.out.println(set.add("a")); // true
System.out.println(set); // [a]
System.out.println(set.add("b")); // true
System.out.println(set); // [a, b]
System.out.println(set.add("c")); // true
System.out.println(set); // [a, b, c]
System.out.println(set.add("a")); // false
System.out.println(set); // [a, b, c]
boolean addAll (Collection<? extends K> c)
final var src = Set.of("a", "b", "c");
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("d", "e", "f"))); // true
System.out.println(set); // [a, b, c, d, e, f]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("a", "b", "c"))); // false
System.out.println(set); // [a, b, c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("a", "c", "e", "f"))); // true
System.out.println(set); // [a, b, c, e, f]
}
final void clear ()
final var set = ConcurrentHashMap.<String>newKeySet();
System.out.println(set); // []
set.add("a");
System.out.println(set); // [a]
set.add("b");
System.out.println(set); // [a, b]
set.clear();
System.out.println(set); // []
boolean contains (Object o)
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]
System.out.println(set.contains("")); // false
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("c")); // true
System.out.println(set.contains("X")); // false
System.out.println(set.contains("abc")); // false
final boolean containsAll (Collection<?> c)
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]
System.out.println(set.containsAll(Set.of())); // true
System.out.println(set.containsAll(Set.of("a"))); // true
System.out.println(set.containsAll(Set.of("a", "b"))); // true
System.out.println(set.containsAll(Set.of("b", "a"))); // true
System.out.println(set.containsAll(Set.of("c", "b", "a"))); // true
System.out.println(set.containsAll(Set.of("X"))); // false
System.out.println(set.containsAll(Set.of("a", "X"))); // false
System.out.println(set.containsAll(Set.of("a", "b", "c", "X"))); // false
boolean equals (Object o)
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]
System.out.println(set.equals(Set.of("a"))); // false
System.out.println(set.equals(Set.of("a", "b"))); // false
System.out.println(set.equals(Set.of("a", "b", "c"))); // true
System.out.println(set.equals(Set.of("c", "b", "a"))); // true
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]
final var hashSet = new HashSet<>(set);
System.out.println(hashSet); // [a, b, c]
System.out.println(set.equals(hashSet)); // true
final var treeSet = new TreeSet<>(set);
System.out.println(treeSet); // [a, b, c]
System.out.println(set.equals(treeSet)); // true
// Set以外はfalse
final var list = new ArrayList<>(set);
System.out.println(list); // [a, b, c]
System.out.println(set.equals(list)); // false
ConcurrentHashMap<K,V> getMap ()
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]
System.out.println(set.getMappedValue()); // true
final var map = set.getMap();
System.out.println(map); // {a=true, b=true, c=true}
set.add("X");
System.out.println(set); // [a, b, c, X]
System.out.println(map); // {a=true, b=true, c=true, X=true}
map.remove("b");
System.out.println(set); // [a, c, X]
System.out.println(map); // {a=true, c=true, X=true}
V getMappedValue ()
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var keys = map.keySet(999);
System.out.println(keys); // [a, b, c]
System.out.println(keys.getMappedValue()); // 999
map.put("d", 4);
System.out.println(map); // {a=1, b=2, c=3, d=4}
System.out.println(keys); // [a, b, c, d]
keys.remove("a");
System.out.println(map); // {b=2, c=3, d=4}
System.out.println(keys); // [b, c, d]
keys.add("X");
System.out.println(map); // {b=2, c=3, d=4, X=999}
System.out.println(keys); // [b, c, d, X]
int hashCode ()
final var set1 = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set1, "a", "b", "c");
System.out.println(set1.hashCode()); // 294
final var set2 = ConcurrentHashMap.<Integer>newKeySet();
Collections.addAll(set2, 1, 2, 3);
System.out.println(set2.hashCode()); // 6
final boolean isEmpty ()
final var set = ConcurrentHashMap.<String>newKeySet();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.isEmpty()); // false
set.clear();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
Iterator<K> iterator ()
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "aaa", "bbb", "ccc");
System.out.println(set); // [aaa, ccc, bbb]
System.out.println("-- iterator --");
set.iterator().forEachRemaining(System.out::println);
// 結果
// ↓
//-- iterator --
//aaa
//ccc
//bbb
boolean remove (Object o)
final var src = Set.of("a", "b", "c");
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("")); // false
System.out.println(set); // [a, b, c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("a")); // true
System.out.println(set); // [b, c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("b")); // true
System.out.println(set); // [a, c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("X")); // false
System.out.println(set); // [a, b, c]
}
boolean removeAll (Collection<?> c)
final var src = Set.of("a", "b", "c");
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of())); // false
System.out.println(set); // [a, b, c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a"))); // true
System.out.println(set); // [b, c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a", "b"))); // true
System.out.println(set); // [c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("b", "a"))); // true
System.out.println(set); // [c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a", "b", "c"))); // true
System.out.println(set); // []
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("X", "Y"))); // false
System.out.println(set); // [a, b, c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a", "X"))); // true
System.out.println(set); // [b, c]
}
final boolean retainAll (Collection<?> c)
final var src = Set.of("a", "b", "c");
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of())); // true
System.out.println(set); // []
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a"))); // true
System.out.println(set); // [a]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a", "b"))); // true
System.out.println(set); // [a, b]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("b", "a"))); // true
System.out.println(set); // [a, b]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a", "b", "c"))); // false
System.out.println(set); // [a, b, c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a", "b", "c", "X", "Y"))); // false
System.out.println(set); // [a, b, c]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("X", "a", "b", "Y"))); // true
System.out.println(set); // [a, b]
}
{
final var set = ConcurrentHashMap.<String>newKeySet();
set.addAll(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("X", "Y"))); // true
System.out.println(set); // []
}
final int size ()
final var set1 = ConcurrentHashMap.<String>newKeySet();
System.out.println(set1); // []
System.out.println(set1.size()); // 0
final var set2 = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set2, "a", "b", "c");
System.out.println(set2); // [a, b, c]
System.out.println(set2.size()); // 3
final Object[] toArray ()
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]
final Object[] array = set.toArray();
System.out.println(Arrays.toString(array)); // [a, b, c]
final <T> T[] toArray (T[] a)
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]
final String[] ret = set.toArray(new String[0]);
System.out.println(Arrays.toString(ret)); // [a, b, c]
final var set = ConcurrentHashMap.<String>newKeySet();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]
{
final String[] array = new String[3];
System.out.println(Arrays.toString(array)); // [null, null, null]
final var ret = set.toArray(array);
System.out.println(Arrays.toString(array)); // [a, b, c]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
{
final String[] array = new String[5];
System.out.println(Arrays.toString(array)); // [null, null, null, null, null]
final var ret = set.toArray(array);
System.out.println(Arrays.toString(array)); // [a, b, c, null, null]
System.out.println(Arrays.toString(ret)); // [a, b, c, null, null]
}
{
final String[] array = new String[1];
System.out.println(Arrays.toString(array)); // [null]
final var ret = set.toArray(array);
System.out.println(Arrays.toString(array)); // [null]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
final String toString ()
final var set = ConcurrentHashMap.<String>newKeySet();
final var str1 = set.toString();
System.out.println(str1); // []
set.add("a");
final var str2 = set.toString();
System.out.println(str2); // [a]
set.add("b");
final var str3 = set.toString();
System.out.println(str3); // [a, b]
set.add("c");
final var str4 = set.toString();
System.out.println(str4); // [a, b, c]
Collectionで宣言されたメソッド
parallelStream, removeIf, stream, toArray
「Java API 使用例 : Collection」をご参照ください。
Iterableで宣言されたメソッド
forEach
「Java API 使用例 : Iterable」をご参照ください。
Setで宣言されたメソッド
clear, containsAll, isEmpty, removeAll, retainAll, size, spliterator, toArray, toArray
「Java API 使用例 : Set」をご参照ください。
関連記事
- API 使用例
- BlockingQueue (ブロッキング・キュー)
- Callable
- CancellationException
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- ConcurrentMap (並列処理用マップ)
- ConcurrentModificationException (並列処理例外)
- ConcurrentSkipListSet (並列処理用セット)
- Condition (同期)
- CopyOnWriteArrayList (並列処理用リスト)
- CopyOnWriteArraySet (並列処理用セット)
- CountDownLatch (同期)
- CyclicBarrier (同期)
- Exchanger (同期)
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- InterruptedException (割込み例外)
- Lock (同期)
- Object (オブジェクト)
- Runnable
- Semaphore (セマフォ)
- Thread (スレッド)
- ThreadGroup
- ThreadLocal
- TimeUnit