Java : LinkedBlockingDeque with Examples

LinkedBlockingDeque (Java SE 20 & JDK 20) API Examples.
You will find code examples on most LinkedBlockingDeque methods.


Summary

An optionally-bounded blocking deque based on linked nodes.

Class diagram

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final var deque = new LinkedBlockingDeque<String>();

    final var future = executor.submit(() -> {
        try {
            while (true) {
                System.out.println("  take ...");
                final var value = deque.takeFirst();

                System.out.printf("  take OK! : value = %s (%f sec.)%n",
                        value, elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.println("  InterruptedException!");
        }
    });

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("aaa");
    deque.putLast("bbb");
    deque.putLast("ccc");

    TimeUnit.SECONDS.sleep(5);

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("XXX");
    deque.putLast("YYY");
    deque.putLast("ZZZ");

    TimeUnit.SECONDS.sleep(5);

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//put values (0.002803 sec.)
//  take ...
//  take OK! : value = aaa (0.004257 sec.)
//  take ...
//  take OK! : value = bbb (0.004373 sec.)
//  take ...
//  take OK! : value = ccc (0.004504 sec.)
//  take ...
//put values (5.005572 sec.)
//  take OK! : value = XXX (5.005799 sec.)
//  take ...
//  take OK! : value = YYY (5.005917 sec.)
//  take ...
//  take OK! : value = ZZZ (5.006012 sec.)
//  take ...
//future.cancel
//  InterruptedException!

Constructors

LinkedBlockingDeque ()

Creates a LinkedBlockingDeque with a capacity of Integer.MAX_VALUE.

final var deque = new LinkedBlockingDeque<String>();
System.out.println(deque); // []
System.out.println(deque.remainingCapacity()); // 2147483647

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]

LinkedBlockingDeque (int capacity)

Creates a LinkedBlockingDeque with the given (fixed) capacity.

final var deque = new LinkedBlockingDeque<String>(3);

System.out.println(deque); // []
System.out.println(deque.size()); // 0
System.out.println(deque.remainingCapacity()); // 3

System.out.println(deque.offerLast("aaa")); // true

System.out.println(deque); // [aaa]
System.out.println(deque.size()); // 1
System.out.println(deque.remainingCapacity()); // 2

System.out.println(deque.offerLast("bbb")); // true

System.out.println(deque); // [aaa, bbb]
System.out.println(deque.size()); // 2
System.out.println(deque.remainingCapacity()); // 1

System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.size()); // 3
System.out.println(deque.remainingCapacity()); // 0

System.out.println(deque.offerLast("XXX")); // false

System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.size()); // 3
System.out.println(deque.remainingCapacity()); // 0

System.out.println(deque.pollFirst()); // aaa

System.out.println(deque); // [bbb, ccc]
System.out.println(deque.size()); // 2
System.out.println(deque.remainingCapacity()); // 1

System.out.println(deque.offerLast("YYY")); // true

System.out.println(deque); // [bbb, ccc, YYY]
System.out.println(deque.size()); // 3
System.out.println(deque.remainingCapacity()); // 0

LinkedBlockingDeque (Collection<? extends E> c)

Creates a LinkedBlockingDeque with a capacity of Integer.MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator.

final var c = List.of("a", "b", "c");

final var deque = new LinkedBlockingDeque<>(c);
System.out.println(deque); // [a, b, c]
System.out.println(deque.size()); // 3

Methods

boolean add (E e)

Inserts the specified element at the end of this deque unless it would violate capacity restrictions.

This method is equivalent to addLast(E e).

boolean addAll (Collection<? extends E> c)

Appends all of the elements in the specified collection to the end of this deque, in the order that they are returned by the specified collection's iterator.

final var deque = new LinkedBlockingDeque<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)

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

// capacity = 3
final var deque = new LinkedBlockingDeque<String>(3);
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]

try {
    deque.addFirst("ddd");
} catch (IllegalStateException e) {
    System.out.println("IllegalStateException! : " + e.getMessage());
}

// Result
// ↓
//IllegalStateException! : Deque full

void addLast (E e)

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

// capacity = 3
final var deque = new LinkedBlockingDeque<String>(3);
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]

try {
    deque.addLast("ddd");
} catch (IllegalStateException e) {
    System.out.println("IllegalStateException! : " + e.getMessage());
}

// Result
// ↓
//IllegalStateException! : Deque full

void clear ()

Atomically removes all of the elements from this deque.

final var deque = new LinkedBlockingDeque<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)

Returns true if this deque contains the specified element.

final var deque = new LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

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 ()

Returns an iterator over the elements in this deque in reverse sequential order.

final var deque = new LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]

final var iterator = deque.descendingIterator();

System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(value -> {
    System.out.println("value = " + value);
});

// Result
// ↓
//-- forEachRemaining --
//value = ccc
//value = bbb
//value = aaa

int drainTo (Collection<? super E> c)

Removes all available elements from this queue and adds them to the given collection.

final var deque = new LinkedBlockingDeque<String>();
final var c = new ArrayList<String>();

System.out.println(deque.offerLast("aaa")); // true

System.out.println(deque); // [aaa]
System.out.println(c); // []

System.out.println(deque.drainTo(c)); // 1

System.out.println(deque); // []
System.out.println(c); // [aaa]

System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [bbb, ccc]
System.out.println(c); // [aaa]

System.out.println(deque.drainTo(c)); // 2

System.out.println(deque); // []
System.out.println(c); // [aaa, bbb, ccc]

int drainTo (Collection<? super E> c, int maxElements)

Removes at most the given number of available elements from this queue and adds them to the given collection.

final var deque = new LinkedBlockingDeque<String>();
final var c = new ArrayList<String>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(c); // []

System.out.println(deque.drainTo(c, 10)); // 3

System.out.println(deque); // []
System.out.println(c); // [aaa, bbb, ccc]

System.out.println(deque.offerLast("XXX")); // true
System.out.println(deque.offerLast("YYY")); // true
System.out.println(deque.offerLast("ZZZ")); // true

System.out.println(deque); // [XXX, YYY, ZZZ]
System.out.println(c); // [aaa, bbb, ccc]

System.out.println(deque.drainTo(c, 2)); // 2

System.out.println(deque); // [ZZZ]
System.out.println(c); // [aaa, bbb, ccc, XXX, YYY]

E element ()

Retrieves, but does not remove, the head of the queue represented by this deque.

This method is equivalent to getFirst().

void forEach (Consumer<? super E> action)

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

final var deque = new LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]

System.out.println("-- forEach --");
deque.forEach(value -> {
    System.out.println("value = " + value);
});

// Result
// ↓
//-- forEach --
//value = aaa
//value = bbb
//value = ccc

E getFirst ()

Retrieves, but does not remove, the first element of this deque.

final var deque = new LinkedBlockingDeque<String>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

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!");
}

// Result
// ↓
//NoSuchElementException!

E getLast ()

Retrieves, but does not remove, the last element of this deque.

final var deque = new LinkedBlockingDeque<String>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

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!");
}

// Result
// ↓
//NoSuchElementException!

Iterator<E> iterator ()

Returns an iterator over the elements in this deque in proper sequence.

final var deque = new LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]

final var iterator = deque.iterator();

System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(value -> {
    System.out.println("value = " + value);
});

// Result
// ↓
//-- forEachRemaining --
//value = aaa
//value = bbb
//value = ccc

boolean offer (E e)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

This method is equivalent to offerLast(E e).

boolean offer (E e, long timeout, TimeUnit unit)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting up to the specified wait time if necessary for space to become available.

This method is equivalent to offerLast(E e, long timeout, TimeUnit unit).

boolean offerFirst (E e)

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

// capacity = 3
final var deque = new LinkedBlockingDeque<String>(3);
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]

System.out.println(deque.offerFirst("ddd")); // false
System.out.println(deque); // [ccc, bbb, aaa]

boolean offerFirst (E e, long timeout, TimeUnit unit)

Inserts the specified element at the front of this deque, waiting up to the specified wait time if necessary for space to become available.

Please see also : offerFirst(E e)

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final var deque = new LinkedBlockingDeque<String>(3);
    System.out.printf("remainingCapacity = %d (%f sec.)%n",
            deque.remainingCapacity(), elapsedTime.getAsDouble());

    executor.submit(() -> {
        try {
            final var list = List.of("aaa", "bbb", "ccc", "ddd");
            for (final var value : list) {
                System.out.println("  offer ...");
                final var ret = deque.offerFirst(value, 5, TimeUnit.SECONDS);

                System.out.printf("  offer ret = %b (%f sec.)%n",
                        ret, elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.println("  InterruptedException!");
        }
    });

    TimeUnit.SECONDS.sleep(10);

    System.out.printf("deque = %s (%f sec.)%n", deque, elapsedTime.getAsDouble());
}

// Result
// ↓
//remainingCapacity = 3 (0.001542 sec.)
//  offer ...
//  offer ret = true (0.004937 sec.)
//  offer ...
//  offer ret = true (0.005130 sec.)
//  offer ...
//  offer ret = true (0.005283 sec.)
//  offer ...
//  offer ret = false (5.006191 sec.)
//deque = [ccc, bbb, aaa] (10.007285 sec.)

boolean offerLast (E e)

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

// capacity = 3
final var deque = new LinkedBlockingDeque<String>(3);
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]

System.out.println(deque.offerLast("ddd")); // false
System.out.println(deque); // [aaa, bbb, ccc]

boolean offerLast (E e, long timeout, TimeUnit unit)

Inserts the specified element at the end of this deque, waiting up to the specified wait time if necessary for space to become available.

Please see also : offerLast(E e)

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final var deque = new LinkedBlockingDeque<String>(3);
    System.out.printf("remainingCapacity = %d (%f sec.)%n",
            deque.remainingCapacity(), elapsedTime.getAsDouble());

    executor.submit(() -> {
        try {
            final var list = List.of("aaa", "bbb", "ccc", "ddd");
            for (final var value : list) {
                System.out.println("  offer ...");
                final var ret = deque.offerLast(value, 5, TimeUnit.SECONDS);

                System.out.printf("  offer ret = %b (%f sec.)%n",
                        ret, elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.println("  InterruptedException!");
        }
    });

    TimeUnit.SECONDS.sleep(10);

    System.out.printf("deque = %s (%f sec.)%n", deque, elapsedTime.getAsDouble());
}

// Result
// ↓
//remainingCapacity = 3 (0.001525 sec.)
//  offer ...
//  offer ret = true (0.004320 sec.)
//  offer ...
//  offer ret = true (0.004461 sec.)
//  offer ...
//  offer ret = true (0.004566 sec.)
//  offer ...
//  offer ret = false (5.013237 sec.)
//deque = [aaa, bbb, ccc] (10.015890 sec.)

E peek ()

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

This method is equivalent to peekFirst().

E peekFirst ()

Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.

final var deque = new LinkedBlockingDeque<String>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

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 ()

Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.

final var deque = new LinkedBlockingDeque<String>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

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 ()

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

This method is equivalent to pollFirst().

E poll (long timeout, TimeUnit unit)

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting up to the specified wait time if necessary for an element to become available.

This method is equivalent to pollFirst(long timeout, TimeUnit unit).

E pollFirst ()

Retrieves and removes the first element of this deque, or returns null if this deque is empty.

final var deque = new LinkedBlockingDeque<String>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

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 pollFirst (long timeout, TimeUnit unit)

Retrieves and removes the first element of this deque, waiting up to the specified wait time if necessary for an element to become available.

Please see also : pollFirst()

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final var deque = new LinkedBlockingDeque<String>();

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("aaa");
    deque.putLast("bbb");
    deque.putLast("ccc");

    final var future = executor.submit(() -> {
        try {
            while (true) {
                System.out.println("  poll ...");
                final var value = deque.pollFirst(5, TimeUnit.SECONDS);

                System.out.printf("  poll value = %s (%f sec.)%n",
                        value, elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.println("  InterruptedException!");
        }
    });

    TimeUnit.SECONDS.sleep(8);

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//put values (0.001490 sec.)
//  poll ...
//  poll value = aaa (0.004407 sec.)
//  poll ...
//  poll value = bbb (0.004601 sec.)
//  poll ...
//  poll value = ccc (0.004791 sec.)
//  poll ...
//  poll value = null (5.019794 sec.)
//  poll ...
//future.cancel
//  InterruptedException!

E pollLast ()

Retrieves and removes the last element of this deque, or returns null if this deque is empty.

final var deque = new LinkedBlockingDeque<String>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

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 pollLast (long timeout, TimeUnit unit)

Retrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available.

Please see also : pollLast()

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final var deque = new LinkedBlockingDeque<String>();

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("aaa");
    deque.putLast("bbb");
    deque.putLast("ccc");

    final var future = executor.submit(() -> {
        try {
            while (true) {
                System.out.println("  poll ...");
                final var value = deque.pollLast(5, TimeUnit.SECONDS);

                System.out.printf("  poll value = %s (%f sec.)%n",
                        value, elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.println("  InterruptedException!");
        }
    });

    TimeUnit.SECONDS.sleep(8);

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//put values (0.001527 sec.)
//  poll ...
//  poll value = ccc (0.004063 sec.)
//  poll ...
//  poll value = bbb (0.004186 sec.)
//  poll ...
//  poll value = aaa (0.004302 sec.)
//  poll ...
//  poll value = null (5.013818 sec.)
//  poll ...
//future.cancel
//  InterruptedException!

E pop ()

Pops an element from the stack represented by this deque.

This method is equivalent to removeFirst().

void push (E e)

Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

This method is equivalent to addFirst(E e).

void put (E e)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting if necessary for space to become available.

This method is equivalent to putLast(E e).

void putFirst (E e)

Inserts the specified element at the front of this deque, waiting if necessary for space to become available.

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final var deque = new LinkedBlockingDeque<String>(3);
    System.out.printf("remainingCapacity = %d (%f sec.)%n",
            deque.remainingCapacity(), elapsedTime.getAsDouble());

    final var future = executor.submit(() -> {
        try {
            final var list = List.of("aaa", "bbb", "ccc", "ddd");
            for (final var value : list) {
                System.out.println("  put ...");
                deque.putFirst(value);

                System.out.printf("  put OK! (%f sec.)%n", elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.printf("  InterruptedException! (%f sec.)%n", elapsedTime.getAsDouble());
        }
    });

    TimeUnit.SECONDS.sleep(5);

    System.out.printf("deque = %s (%f sec.)%n", deque, elapsedTime.getAsDouble());

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//remainingCapacity = 3 (0.001544 sec.)
//  put ...
//  put OK! (0.006679 sec.)
//  put ...
//  put OK! (0.006793 sec.)
//  put ...
//  put OK! (0.006899 sec.)
//  put ...
//deque = [ccc, bbb, aaa] (5.010734 sec.)
//future.cancel
//  InterruptedException! (5.011484 sec.)

void putLast (E e)

Inserts the specified element at the end of this deque, waiting if necessary for space to become available.

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final var deque = new LinkedBlockingDeque<String>(3);
    System.out.printf("remainingCapacity = %d (%f sec.)%n",
            deque.remainingCapacity(), elapsedTime.getAsDouble());

    final var future = executor.submit(() -> {
        try {
            final var list = List.of("aaa", "bbb", "ccc", "ddd");
            for (final var value : list) {
                System.out.println("  put ...");
                deque.putLast(value);

                System.out.printf("  put OK! (%f sec.)%n", elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.printf("  InterruptedException! (%f sec.)%n", elapsedTime.getAsDouble());
        }
    });

    TimeUnit.SECONDS.sleep(5);

    System.out.printf("deque = %s (%f sec.)%n", deque, elapsedTime.getAsDouble());

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//remainingCapacity = 3 (0.001664 sec.)
//  put ...
//  put OK! (0.006416 sec.)
//  put ...
//  put OK! (0.006584 sec.)
//  put ...
//  put OK! (0.006753 sec.)
//  put ...
//deque = [aaa, bbb, ccc] (5.021150 sec.)
//future.cancel
//  InterruptedException! (5.024899 sec.)

int remainingCapacity ()

Returns the number of additional elements that this deque can ideally (in the absence of memory or resource constraints) accept without blocking.

final var deque = new LinkedBlockingDeque<String>(3);

System.out.println(deque); // []
System.out.println(deque.remainingCapacity()); // 3

System.out.println(deque.offerLast("aaa")); // true

System.out.println(deque); // [aaa]
System.out.println(deque.remainingCapacity()); // 2

System.out.println(deque.offerLast("bbb")); // true

System.out.println(deque); // [aaa, bbb]
System.out.println(deque.remainingCapacity()); // 1

System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.remainingCapacity()); // 0

deque.clear();

System.out.println(deque); // []
System.out.println(deque.remainingCapacity()); // 3

E remove ()

Retrieves and removes the head of the queue represented by this deque.

This method is equivalent to removeFirst().

boolean remove (Object o)

Removes the first occurrence of the specified element from this deque.

This method is equivalent to removeFirstOccurrence(Object o).

boolean removeAll (Collection<?> c)

Removes all of this collection's elements that are also contained in the specified collection (optional operation).

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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 ()

Retrieves and removes the first element of this deque.

final var deque = new LinkedBlockingDeque<String>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

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!");
}

// Result
// ↓
//NoSuchElementException!

boolean removeFirstOccurrence (Object o)

Removes the first occurrence of the specified element from this deque.

final var deque = new LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("BBB")); // true
System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("BBB")); // true
System.out.println(deque.offerLast("ccc")); // true

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("XXX")); // false
System.out.println(deque); // [aaa, BBB, ccc]

boolean removeIf (Predicate<? super E> filter)

Removes all of the elements of this collection that satisfy the given predicate.

final var deque = new LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("BBB")); // true
System.out.println(deque.offerLast("ccc")); // true
System.out.println(deque.offerLast("DDD")); // true

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 LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true

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 ()

Retrieves and removes the last element of this deque.

final var deque = new LinkedBlockingDeque<String>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

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!");
}

// Result
// ↓
//NoSuchElementException!

boolean removeLastOccurrence (Object o)

Removes the last occurrence of the specified element from this deque.

final var deque = new LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("BBB")); // true
System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("BBB")); // true
System.out.println(deque.offerLast("ccc")); // true

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("XXX")); // false
System.out.println(deque); // [aaa, BBB, ccc]

boolean retainAll (Collection<?> c)

Retains only the elements in this collection that are contained in the specified collection (optional operation).

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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 LinkedBlockingDeque<>(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 ()

Returns the number of elements in this deque.

final var deque = new LinkedBlockingDeque<String>();
System.out.println(deque); // []
System.out.println(deque.size()); // 0

System.out.println(deque.offerLast("aaa")); // true

System.out.println(deque); // [aaa]
System.out.println(deque.size()); // 1

System.out.println(deque.offerLast("bbb")); // true

System.out.println(deque); // [aaa, bbb]
System.out.println(deque.size()); // 2

System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.size()); // 3

Spliterator<E> spliterator ()

Returns a Spliterator over the elements in this deque.

final var deque = new LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]

final var spliterator = deque.spliterator();

System.out.println("-- forEachRemaining --");
spliterator.forEachRemaining(value -> {
    System.out.println("value = " + value);
});

// Result
// ↓
//-- forEachRemaining --
//value = aaa
//value = bbb
//value = ccc

E take ()

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting if necessary until an element becomes available.

This method is equivalent to takeFirst().

E takeFirst ()

Retrieves and removes the first element of this deque, waiting if necessary until an element becomes available.

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final var deque = new LinkedBlockingDeque<String>();

    final var future = executor.submit(() -> {
        try {
            while (true) {
                System.out.println("  take ...");
                final var value = deque.takeFirst();

                System.out.printf("  take OK! : value = %s (%f sec.)%n",
                        value, elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.println("  InterruptedException!");
        }
    });

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("aaa");
    deque.putLast("bbb");
    deque.putLast("ccc");

    TimeUnit.SECONDS.sleep(5);

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("XXX");
    deque.putLast("YYY");
    deque.putLast("ZZZ");

    TimeUnit.SECONDS.sleep(5);

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//put values (0.002803 sec.)
//  take ...
//  take OK! : value = aaa (0.004257 sec.)
//  take ...
//  take OK! : value = bbb (0.004373 sec.)
//  take ...
//  take OK! : value = ccc (0.004504 sec.)
//  take ...
//put values (5.005572 sec.)
//  take OK! : value = XXX (5.005799 sec.)
//  take ...
//  take OK! : value = YYY (5.005917 sec.)
//  take ...
//  take OK! : value = ZZZ (5.006012 sec.)
//  take ...
//future.cancel
//  InterruptedException!

E takeLast ()

Retrieves and removes the last element of this deque, waiting if necessary until an element becomes available.

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final var deque = new LinkedBlockingDeque<String>();

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("aaa");
    deque.putLast("bbb");
    deque.putLast("ccc");

    final var future = executor.submit(() -> {
        try {
            while (true) {
                System.out.println("  take ...");
                final var value = deque.takeLast();

                System.out.printf("  take OK! : value = %s (%f sec.)%n",
                        value, elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.println("  InterruptedException!");
        }
    });

    TimeUnit.SECONDS.sleep(5);

    System.out.printf("future.cancel (%f sec.)%n", elapsedTime.getAsDouble());
    future.cancel(true);
}

// Result
// ↓
//put values (0.001486 sec.)
//  take ...
//  take OK! : value = ccc (0.005003 sec.)
//  take ...
//  take OK! : value = bbb (0.005162 sec.)
//  take ...
//  take OK! : value = aaa (0.005336 sec.)
//  take ...
//future.cancel (5.019323 sec.)
//  InterruptedException!

Object[] toArray ()

Returns an array containing all of the elements in this deque, in proper sequence (from first to last element).

final var deque = new LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("a")); // true
System.out.println(deque.offerLast("b")); // true
System.out.println(deque.offerLast("c")); // true

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)

Returns an array containing all of the elements in this deque, in proper sequence; the runtime type of the returned array is that of the specified array.

final var deque = new LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("a")); // true
System.out.println(deque.offerLast("b")); // true
System.out.println(deque.offerLast("c")); // true

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 LinkedBlockingDeque<String>();

System.out.println(deque.offerLast("a")); // true
System.out.println(deque.offerLast("b")); // true
System.out.println(deque.offerLast("c")); // true

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)); // [null]
    System.out.println(Arrays.toString(ret)); // [a, b, c]
}

Methods declared in AbstractCollection

containsAll, isEmpty, toString

Please see the link below.

Methods declared in Collection

containsAll, equals, hashCode, isEmpty, parallelStream, stream, toArray

Please see the link below.


Related posts

To top of page