Java : ConcurrentLinkedDeque (並列処理用・両端キュー) - API使用例
ConcurrentLinkedDeque (Java SE 20 & JDK 20) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
ConcurrentLinkedDeque クラスは、反復処理 (例えば for-eachループ文) 中に add や remove などによる変更を許容する Deque の実装です。
非チェック例外の ConcurrentModificationException が発生しないことが保証されています。
void test(Deque<Integer> deque) throws InterruptedException {
for (int i = 0; i < 5; i++) {
deque.addLast(i);
}
try (final var executor = Executors.newSingleThreadScheduledExecutor()) {
executor.schedule(() -> {
System.out.println("-- add value! --");
deque.addLast(9999);
}, 5, TimeUnit.SECONDS);
try {
for (final var value : deque) {
System.out.println("value = " + value);
TimeUnit.SECONDS.sleep(2);
}
} catch (ConcurrentModificationException e) {
System.out.println("ConcurrentModificationException!");
}
}
System.out.println("-- end --");
System.out.println("deque = " + deque);
}
test(new LinkedList<>());
// 結果
// ↓
//value = 0
//value = 1
//value = 2
//-- add value! --
//ConcurrentModificationException!
//-- end --
//deque = [0, 1, 2, 3, 4, 9999]
test(new ConcurrentLinkedDeque<>());
// 結果
// ↓
//value = 0
//value = 1
//value = 2
//-- add value! --
//value = 3
//value = 4
//value = 9999
//-- end --
//deque = [0, 1, 2, 3, 4, 9999]
コンストラクタ
ConcurrentLinkedDeque ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
System.out.println(deque.isEmpty()); // true
deque.addLast("aaa");
deque.addLast("bbb");
deque.addLast("ccc");
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.size()); // 3
ConcurrentLinkedDeque (Collection<? extends E> c)
final var c = List.of("a", "b", "c");
final var deque = new ConcurrentLinkedDeque<>(c);
System.out.println(deque); // [a, b, c]
System.out.println(deque.size()); // 3
メソッド
boolean add (E e)
このメソッドは addLast(E e) と同等です。
API使用例はそちらをご参照ください。
boolean addAll (Collection<? extends E> c)
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
System.out.println(deque.addAll(List.of("a1"))); // true
System.out.println(deque); // [a1]
System.out.println(deque.addAll(List.of("b1", "b2"))); // true
System.out.println(deque); // [a1, b1, b2]
System.out.println(deque.addAll(List.of("c1", "c2", "c3"))); // true
System.out.println(deque); // [a1, b1, b2, c1, c2, c3]
void addFirst (E e)
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.addFirst("aaa");
System.out.println(deque); // [aaa]
deque.addFirst("bbb");
System.out.println(deque); // [bbb, aaa]
deque.addFirst("ccc");
System.out.println(deque); // [ccc, bbb, aaa]
void addLast (E e)
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.addLast("aaa");
System.out.println(deque); // [aaa]
deque.addLast("bbb");
System.out.println(deque); // [aaa, bbb]
deque.addLast("ccc");
System.out.println(deque); // [aaa, bbb, ccc]
void clear ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.addLast("a");
System.out.println(deque); // [a]
deque.addLast("b");
System.out.println(deque); // [a, b]
deque.clear();
System.out.println(deque); // []
boolean contains (Object o)
final var deque = new ConcurrentLinkedDeque<String>();
Collections.addAll(deque, "aaa", "bbb", "ccc");
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.contains("aaa")); // true
System.out.println(deque.contains("bbb")); // true
System.out.println(deque.contains("XXX")); // false
Iterator<E> descendingIterator ()
final var deque = new ConcurrentLinkedDeque<String>();
Collections.addAll(deque, "aaa", "bbb", "ccc");
System.out.println(deque); // [aaa, bbb, ccc]
final var iterator = deque.descendingIterator();
System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(value -> {
System.out.println("value = " + value);
});
// 結果
// ↓
//-- forEachRemaining --
//value = ccc
//value = bbb
//value = aaa
E element ()
このメソッドは getFirst() と同等です。
API使用例はそちらをご参照ください。
void forEach (Consumer<? super E> action)
final var deque = new ConcurrentLinkedDeque<String>();
Collections.addAll(deque, "aaa", "bbb", "ccc");
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println("-- forEach --");
deque.forEach(value -> {
System.out.println("value = " + value);
});
// 結果
// ↓
//-- forEach --
//value = aaa
//value = bbb
//value = ccc
E getFirst ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.addLast("aaa");
deque.addLast("bbb");
deque.addLast("ccc");
System.out.println(deque.getFirst()); // aaa
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.removeFirst()); // aaa
System.out.println(deque); // [bbb, ccc]
System.out.println(deque.getFirst()); // bbb
System.out.println(deque); // [bbb, ccc]
System.out.println(deque.removeFirst()); // bbb
System.out.println(deque); // [ccc]
System.out.println(deque.getFirst()); // ccc
System.out.println(deque); // [ccc]
System.out.println(deque.removeFirst()); // ccc
System.out.println(deque); // []
try {
deque.getFirst();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// 結果
// ↓
//NoSuchElementException!
E getLast ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.addLast("aaa");
deque.addLast("bbb");
deque.addLast("ccc");
System.out.println(deque.getLast()); // ccc
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.removeLast()); // ccc
System.out.println(deque); // [aaa, bbb]
System.out.println(deque.getLast()); // bbb
System.out.println(deque); // [aaa, bbb]
System.out.println(deque.removeLast()); // bbb
System.out.println(deque); // [aaa]
System.out.println(deque.getLast()); // aaa
System.out.println(deque); // [aaa]
System.out.println(deque.removeLast()); // aaa
System.out.println(deque); // []
try {
deque.getLast();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// 結果
// ↓
//NoSuchElementException!
boolean isEmpty ()
final var deque = new ConcurrentLinkedDeque<String>();
deque.addLast("a");
deque.addLast("b");
deque.addLast("c");
System.out.println(deque); // [a, b, c]
System.out.println(deque.isEmpty()); // false
deque.clear();
System.out.println(deque); // []
System.out.println(deque.isEmpty()); // true
Iterator<E> iterator ()
final var deque = new ConcurrentLinkedDeque<String>();
Collections.addAll(deque, "aaa", "bbb", "ccc");
System.out.println(deque); // [aaa, bbb, ccc]
final var iterator = deque.iterator();
System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(value -> {
System.out.println("value = " + value);
});
// 結果
// ↓
//-- forEachRemaining --
//value = aaa
//value = bbb
//value = ccc
boolean offer (E e)
このメソッドは offerLast(E e) と同等です。
API使用例はそちらをご参照ください。
boolean offerFirst (E e)
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
System.out.println(deque.offerFirst("aaa")); // true
System.out.println(deque); // [aaa]
System.out.println(deque.offerFirst("bbb")); // true
System.out.println(deque); // [bbb, aaa]
System.out.println(deque.offerFirst("ccc")); // true
System.out.println(deque); // [ccc, bbb, aaa]
boolean offerLast (E e)
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque); // [aaa]
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque); // [aaa, bbb]
System.out.println(deque.offerLast("ccc")); // true
System.out.println(deque); // [aaa, bbb, ccc]
E peek ()
このメソッドは peekFirst() と同等です。
API使用例はそちらをご参照ください。
E peekFirst ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.addLast("aaa");
deque.addLast("bbb");
deque.addLast("ccc");
System.out.println(deque.peekFirst()); // aaa
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.pollFirst()); // aaa
System.out.println(deque); // [bbb, ccc]
System.out.println(deque.peekFirst()); // bbb
System.out.println(deque); // [bbb, ccc]
System.out.println(deque.pollFirst()); // bbb
System.out.println(deque); // [ccc]
System.out.println(deque.peekFirst()); // ccc
System.out.println(deque); // [ccc]
System.out.println(deque.pollFirst()); // ccc
System.out.println(deque); // []
System.out.println(deque.peekFirst()); // null
System.out.println(deque); // []
E peekLast ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.addLast("aaa");
deque.addLast("bbb");
deque.addLast("ccc");
System.out.println(deque.peekLast()); // ccc
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.pollLast()); // ccc
System.out.println(deque); // [aaa, bbb]
System.out.println(deque.peekLast()); // bbb
System.out.println(deque); // [aaa, bbb]
System.out.println(deque.pollLast()); // bbb
System.out.println(deque); // [aaa]
System.out.println(deque.peekLast()); // aaa
System.out.println(deque); // [aaa]
System.out.println(deque.pollLast()); // aaa
System.out.println(deque); // []
System.out.println(deque.peekLast()); // null
System.out.println(deque); // []
E poll ()
このメソッドは pollFirst() と同等です。
API使用例はそちらをご参照ください。
E pollFirst ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.add("aaa");
deque.add("bbb");
deque.add("ccc");
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.pollFirst()); // aaa
System.out.println(deque); // [bbb, ccc]
System.out.println(deque.pollFirst()); // bbb
System.out.println(deque); // [ccc]
System.out.println(deque.pollFirst()); // ccc
System.out.println(deque); // []
System.out.println(deque.pollFirst()); // null
System.out.println(deque); // []
E pollLast ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.add("aaa");
deque.add("bbb");
deque.add("ccc");
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.pollLast()); // ccc
System.out.println(deque); // [aaa, bbb]
System.out.println(deque.pollLast()); // bbb
System.out.println(deque); // [aaa]
System.out.println(deque.pollLast()); // aaa
System.out.println(deque); // []
System.out.println(deque.pollLast()); // null
System.out.println(deque); // []
E pop ()
このメソッドは removeFirst() と同等です。
API使用例はそちらをご参照ください。
void push (E e)
このメソッドは addFirst(E e) と同等です。
API使用例はそちらをご参照ください。
E remove ()
このメソッドは removeFirst() と同等です。
API使用例はそちらをご参照ください。
boolean remove (Object o)
このメソッドは removeFirstOccurrence(Object o) と同等です。
API使用例はそちらをご参照ください。
boolean removeAll (Collection<?> c)
final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.removeAll(List.of())); // false
System.out.println(deque); // [a, b, a, b, A, B]
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.removeAll(List.of("a"))); // true
System.out.println(deque); // [b, b, A, B]
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.removeAll(List.of("a", "b"))); // true
System.out.println(deque); // [A, B]
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.removeAll(List.of("b", "a"))); // true
System.out.println(deque); // [A, B]
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.removeAll(List.of("A"))); // true
System.out.println(deque); // [a, b, a, b, B]
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.removeAll(List.of("X", "Y", "Z"))); // false
System.out.println(deque); // [a, b, a, b, A, B]
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.removeAll(List.of("A", "X", "Y", "Z"))); // true
System.out.println(deque); // [a, b, a, b, B]
}
E removeFirst ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.add("aaa");
deque.add("bbb");
deque.add("ccc");
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.removeFirst()); // aaa
System.out.println(deque); // [bbb, ccc]
System.out.println(deque.removeFirst()); // bbb
System.out.println(deque); // [ccc]
System.out.println(deque.removeFirst()); // ccc
System.out.println(deque); // []
try {
deque.removeFirst();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// 結果
// ↓
//NoSuchElementException!
boolean removeFirstOccurrence (Object o)
final var deque = new ConcurrentLinkedDeque<String>();
Collections.addAll(deque, "aaa", "BBB", "aaa", "BBB", "ccc");
System.out.println(deque); // [aaa, BBB, aaa, BBB, ccc]
System.out.println(deque.removeFirstOccurrence("aaa")); // true
System.out.println(deque); // [BBB, aaa, BBB, ccc]
System.out.println(deque.removeFirstOccurrence("BBB")); // true
System.out.println(deque); // [aaa, BBB, ccc]
System.out.println(deque.removeFirstOccurrence("BBB")); // true
System.out.println(deque); // [aaa, ccc]
System.out.println(deque.removeFirstOccurrence("XXX")); // false
System.out.println(deque); // [aaa, ccc]
boolean removeIf (Predicate<? super E> filter)
final var deque = new ConcurrentLinkedDeque<String>();
deque.addLast("aaa");
deque.addLast("BBB");
deque.addLast("ccc");
deque.addLast("DDD");
System.out.println(deque); // [aaa, BBB, ccc, DDD]
final var ret = deque.removeIf(s -> {
// 大文字であるかチェックします。
return s.equals(s.toUpperCase());
});
System.out.println(ret); // true
System.out.println(deque); // [aaa, ccc]
final var deque = new ConcurrentLinkedDeque<String>();
deque.addLast("aaa");
deque.addLast("bbb");
System.out.println(deque); // [aaa, bbb]
final var ret = deque.removeIf(s -> s.equals(s.toUpperCase()));
System.out.println(ret); // false
System.out.println(deque); // [aaa, bbb]
E removeLast ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
deque.add("aaa");
deque.add("bbb");
deque.add("ccc");
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.removeLast()); // ccc
System.out.println(deque); // [aaa, bbb]
System.out.println(deque.removeLast()); // bbb
System.out.println(deque); // [aaa]
System.out.println(deque.removeLast()); // aaa
System.out.println(deque); // []
try {
deque.removeLast();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// 結果
// ↓
//NoSuchElementException!
boolean removeLastOccurrence (Object o)
final var deque = new ConcurrentLinkedDeque<String>();
Collections.addAll(deque, "aaa", "BBB", "aaa", "BBB", "ccc");
System.out.println(deque); // [aaa, BBB, aaa, BBB, ccc]
System.out.println(deque.removeLastOccurrence("BBB")); // true
System.out.println(deque); // [aaa, BBB, aaa, ccc]
System.out.println(deque.removeLastOccurrence("aaa")); // true
System.out.println(deque); // [aaa, BBB, ccc]
System.out.println(deque.removeLastOccurrence("aaa")); // true
System.out.println(deque); // [BBB, ccc]
System.out.println(deque.removeLastOccurrence("XXX")); // false
System.out.println(deque); // [BBB, ccc]
boolean retainAll (Collection<?> c)
final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.retainAll(List.of())); // true
System.out.println(deque); // []
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.retainAll(List.of("a", "b"))); // true
System.out.println(deque); // [a, b, a, b]
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.retainAll(List.of("b", "a"))); // true
System.out.println(deque); // [a, b, a, b]
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.retainAll(List.of("A"))); // true
System.out.println(deque); // [A]
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.retainAll(List.of("X", "Y", "Z"))); // true
System.out.println(deque); // []
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.retainAll(List.of("A", "X", "Y", "Z"))); // true
System.out.println(deque); // [A]
}
{
final var deque = new ConcurrentLinkedDeque<>(src);
System.out.println(deque); // [a, b, a, b, A, B]
System.out.println(deque.retainAll(List.of("a", "b", "A", "B"))); // false
System.out.println(deque); // [a, b, a, b, A, B]
}
int size ()
final var deque = new ConcurrentLinkedDeque<String>();
System.out.println(deque); // []
System.out.println(deque.size()); // 0
deque.addLast("aaa");
System.out.println(deque); // [aaa]
System.out.println(deque.size()); // 1
deque.addLast("bbb");
System.out.println(deque); // [aaa, bbb]
System.out.println(deque.size()); // 2
deque.addLast("ccc");
System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.size()); // 3
Spliterator<E> spliterator ()
final var deque = new ConcurrentLinkedDeque<String>();
Collections.addAll(deque, "aaa", "bbb", "ccc");
System.out.println(deque); // [aaa, bbb, ccc]
final var spliterator = deque.spliterator();
System.out.println("-- forEachRemaining --");
spliterator.forEachRemaining(value -> {
System.out.println("value = " + value);
});
// 結果
// ↓
//-- forEachRemaining --
//value = aaa
//value = bbb
//value = ccc
Object[] toArray ()
final var deque = new ConcurrentLinkedDeque<String>();
Collections.addAll(deque, "a", "b", "c");
System.out.println(deque); // [a, b, c]
final Object[] array = deque.toArray();
System.out.println(Arrays.toString(array)); // [a, b, c]
<T> T[] toArray (T[] a)
final var deque = new ConcurrentLinkedDeque<String>();
Collections.addAll(deque, "a", "b", "c");
System.out.println(deque); // [a, b, c]
final String[] array = deque.toArray(new String[0]);
System.out.println(Arrays.toString(array)); // [a, b, c]
final var deque = new ConcurrentLinkedDeque<String>();
Collections.addAll(deque, "a", "b", "c");
System.out.println(deque); // [a, b, c]
{
final String[] array = new String[3];
System.out.println(Arrays.toString(array)); // [null, null, null]
final var ret = deque.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 = deque.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 = deque.toArray(array);
System.out.println(Arrays.toString(array)); // [a]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
AbstractCollectionで宣言されたメソッド
containsAll, toString
「Java API 使用例 : AbstractCollection」をご参照ください。
Collectionで宣言されたメソッド
containsAll, equals, hashCode, parallelStream, stream, toArray
「Java API 使用例 : Collection」をご参照ください。
関連記事
- API 使用例
- BlockingQueue (ブロッキング・キュー)
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView (並列処理用セット)
- 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