Java : CopyOnWriteArraySet (並列処理用セット) - API使用例
CopyOnWriteArraySet (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
CopyOnWriteArraySet クラスは、反復処理 (例えば for-eachループ文) 中に add や remove などによる変更を許容する Set の実装です。
内部的には CopyOnWriteArrayList が使われているため、LinkedHashSet のように要素の追加順を保持します。
void test(Set<Integer> set) throws InterruptedException {
for (int i = 0; i < 5; i++) {
set.add(i);
}
try (final var executor = Executors.newSingleThreadScheduledExecutor()) {
executor.schedule(() -> {
System.out.println("-- add value! --");
set.add(9999);
}, 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 LinkedHashSet<>());
// 結果
// ↓
//value = 0
//value = 1
//value = 2
//-- add value! --
//ConcurrentModificationException!
//-- end --
//set = [0, 1, 2, 3, 4, 9999]
test(new CopyOnWriteArraySet<>());
// 結果
// ↓
//value = 0
//value = 1
//value = 2
//-- add value! --
//value = 3
//value = 4
//-- end --
//set = [0, 1, 2, 3, 4, 9999]
コンストラクタ
CopyOnWriteArraySet ()
final var set = new CopyOnWriteArraySet<String>();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
CopyOnWriteArraySet (Collection<? extends E> c)
final var c = List.of("a", "b", "c");
final var set = new CopyOnWriteArraySet<>(c);
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
メソッド
boolean add (E e)
final var set = new CopyOnWriteArraySet<String>();
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 E> c)
final var src = List.of("a", "b", "c");
{
final var set = new CopyOnWriteArraySet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(List.of("d", "e", "f"))); // true
System.out.println(set); // [a, b, c, d, e, f]
}
{
final var set = new CopyOnWriteArraySet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(List.of("a", "b", "c"))); // false
System.out.println(set); // [a, b, c]
}
{
final var set = new CopyOnWriteArraySet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(List.of("a", "c", "e", "f"))); // true
System.out.println(set); // [a, b, c, e, f]
}
void clear ()
final var set = new CopyOnWriteArraySet<String>();
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 = new CopyOnWriteArraySet<String>();
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
boolean containsAll (Collection<?> c)
final var set = new CopyOnWriteArraySet<String>();
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 = new CopyOnWriteArraySet<String>();
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 = new CopyOnWriteArraySet<String>();
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
void forEach (Consumer<? super E> action)
final var set = new CopyOnWriteArraySet<String>();
Collections.addAll(set, "aaa", "bbb", "ccc");
System.out.println(set); // [aaa, bbb, ccc]
System.out.println("-- forEach --");
set.forEach(element -> {
System.out.println(element);
});
// 結果
// ↓
//-- forEach --
//aaa
//bbb
//ccc
boolean isEmpty ()
final var set = new CopyOnWriteArraySet<String>();
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<E> iterator ()
final var set = new CopyOnWriteArraySet<String>();
Collections.addAll(set, "aaa", "bbb", "ccc");
System.out.println(set); // [aaa, bbb, ccc]
System.out.println("-- iterator --");
set.iterator().forEachRemaining(System.out::println);
// 結果
// ↓
//-- iterator --
//aaa
//bbb
//ccc
boolean remove (Object o)
final var src = List.of("a", "b", "c");
{
final var set = new CopyOnWriteArraySet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("")); // false
System.out.println(set); // [a, b, c]
}
{
final var set = new CopyOnWriteArraySet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("a")); // true
System.out.println(set); // [b, c]
}
{
final var set = new CopyOnWriteArraySet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("b")); // true
System.out.println(set); // [a, c]
}
{
final var set = new CopyOnWriteArraySet<>(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 = List.of("a", "b", "c");
{
final var set = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a", "X"))); // true
System.out.println(set); // [b, c]
}
boolean removeIf (Predicate<? super E> filter)
final var set = new CopyOnWriteArraySet<String>();
set.add("aaa");
set.add("BBB");
set.add("ccc");
set.add("DDD");
System.out.println(set); // [aaa, BBB, ccc, DDD]
final var ret = set.removeIf(s -> {
// 大文字であるかチェックします。
return s.equals(s.toUpperCase());
});
System.out.println(ret); // true
System.out.println(set); // [aaa, ccc]
final var set = new CopyOnWriteArraySet<String>();
set.add("aaa");
set.add("bbb");
System.out.println(set); // [aaa, bbb]
final var ret = set.removeIf(s -> s.equals(s.toUpperCase()));
System.out.println(ret); // false
System.out.println(set); // [aaa, bbb]
boolean retainAll (Collection<?> c)
final var src = List.of("a", "b", "c");
{
final var set = new CopyOnWriteArraySet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of())); // true
System.out.println(set); // []
}
{
final var set = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(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 = new CopyOnWriteArraySet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("X", "Y"))); // true
System.out.println(set); // []
}
int size ()
final var set1 = new CopyOnWriteArraySet<String>();
System.out.println(set1); // []
System.out.println(set1.size()); // 0
final var set2 = new CopyOnWriteArraySet<String>();
Collections.addAll(set2, "a", "b", "c");
System.out.println(set2); // [a, b, c]
System.out.println(set2.size()); // 3
Spliterator<E> spliterator ()
final var set = new CopyOnWriteArraySet<String>();
Collections.addAll(set, "aaa", "bbb", "ccc");
System.out.println(set); // [aaa, bbb, ccc]
final var spliterator = set.spliterator();
spliterator.forEachRemaining(System.out::println);
// 結果
// ↓
//aaa
//bbb
//ccc
Object[] toArray ()
final var set = new CopyOnWriteArraySet<String>();
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]
<T> T[] toArray (T[] a)
final var set = new CopyOnWriteArraySet<String>();
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 = new CopyOnWriteArraySet<String>();
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]
}
AbstractSetで宣言されたメソッド
hashCode
「Java API 使用例 : AbstractSet」をご参照ください。
AbstractCollectionで宣言されたメソッド
toString
「Java API 使用例 : AbstractCollection」をご参照ください。
Collectionで宣言されたメソッド
parallelStream, stream, toArray
「Java API 使用例 : Collection」をご参照ください。
関連記事
- API 使用例
- BlockingQueue (ブロッキング・キュー)
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView (並列処理用セット)
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- ConcurrentMap (並列処理用マップ)
- ConcurrentModificationException (並列処理例外)
- ConcurrentSkipListSet (並列処理用セット)
- Condition (同期)
- CopyOnWriteArrayList (並列処理用リスト)
- CountDownLatch (同期)
- CyclicBarrier (同期)
- Exchanger (同期)
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- InterruptedException (割込み例外)
- Lock (同期)
- Object (オブジェクト)
- Runnable
- Semaphore (セマフォ)
- Thread (スレッド)
- ThreadGroup
- ThreadLocal
- TimeUnit