Java : LinkedBlockingQueue with Examples

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


Summary

An optionally-bounded blocking queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time.

Class diagram

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

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

    final var queue = new LinkedBlockingQueue<String>();

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

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

    queue.put("aaa");
    queue.put("bbb");
    queue.put("ccc");

    TimeUnit.SECONDS.sleep(5);

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

    queue.put("XXX");
    queue.put("YYY");
    queue.put("ZZZ");

    TimeUnit.SECONDS.sleep(5);

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

// Result
// ↓
//put values (0.002790 sec.)
//  take ...
//  take OK! : value = aaa (0.006541 sec.)
//  take ...
//  take OK! : value = bbb (0.006858 sec.)
//  take ...
//  take OK! : value = ccc (0.007065 sec.)
//  take ...
//put values (5.017256 sec.)
//  take OK! : value = XXX (5.017574 sec.)
//  take ...
//  take OK! : value = YYY (5.017692 sec.)
//  take ...
//  take OK! : value = ZZZ (5.017783 sec.)
//  take ...
//future.cancel
//  InterruptedException!

Constructors

LinkedBlockingQueue ()

Creates a LinkedBlockingQueue with a capacity of Integer.MAX_VALUE.

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

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true

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

LinkedBlockingQueue (int capacity)

Creates a LinkedBlockingQueue with the given (fixed) capacity.

final var queue = new LinkedBlockingQueue<String>(3);

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

System.out.println(queue.offer("aaa")); // true

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

System.out.println(queue.offer("bbb")); // true

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

System.out.println(queue.offer("ccc")); // true

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

System.out.println(queue.offer("XXX")); // false

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

System.out.println(queue.poll()); // aaa

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

System.out.println(queue.offer("YYY")); // true

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

LinkedBlockingQueue (Collection<? extends E> c)

Creates a LinkedBlockingQueue 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 queue = new LinkedBlockingQueue<>(c);
System.out.println(queue); // [a, b, c]
System.out.println(queue.size()); // 3

Methods

void clear ()

Atomically removes all of the elements from this queue.

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

System.out.println(queue.offer("a")); // true
System.out.println(queue); // [a]

System.out.println(queue.offer("b")); // true
System.out.println(queue); // [a, b]

queue.clear();
System.out.println(queue); // []

boolean contains (Object o)

Returns true if this queue contains the specified element.

final var queue = new LinkedBlockingQueue<String>();

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true

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

System.out.println(queue.contains("aaa")); // true
System.out.println(queue.contains("bbb")); // true
System.out.println(queue.contains("XXX")); // false

int drainTo (Collection<? super E> c)

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

final var queue = new LinkedBlockingQueue<String>();
final var c = new ArrayList<String>();

System.out.println(queue.offer("aaa")); // true

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

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

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

System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true

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

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

System.out.println(queue); // []
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 queue = new LinkedBlockingQueue<String>();
final var c = new ArrayList<String>();

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true

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

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

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

System.out.println(queue.offer("XXX")); // true
System.out.println(queue.offer("YYY")); // true
System.out.println(queue.offer("ZZZ")); // true

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

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

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

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 queue = new LinkedBlockingQueue<String>();

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true

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

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

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

Iterator<E> iterator ()

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

final var queue = new LinkedBlockingQueue<String>();

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true

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

final var iterator = queue.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 at the tail of this queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and false if this queue is full.

// capacity = 3
final var queue = new LinkedBlockingQueue<String>(3);
System.out.println(queue); // []

System.out.println(queue.offer("aaa")); // true
System.out.println(queue); // [aaa]

System.out.println(queue.offer("bbb")); // true
System.out.println(queue); // [aaa, bbb]

System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]

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

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

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

Please see also : offer(E e)

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

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

    final var queue = new LinkedBlockingQueue<String>(3);
    System.out.printf("remainingCapacity = %d (%f sec.)%n",
            queue.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 = queue.offer(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("queue = %s (%f sec.)%n", queue, elapsedTime.getAsDouble());
}

// Result
// ↓
//remainingCapacity = 3 (0.001546 sec.)
//  offer ...
//  offer ret = true (0.004601 sec.)
//  offer ...
//  offer ret = true (0.004768 sec.)
//  offer ...
//  offer ret = true (0.004909 sec.)
//  offer ...
//  offer ret = false (5.007159 sec.)
//queue = [aaa, bbb, ccc] (10.012891 sec.)

E peek ()

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

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

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true

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

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

System.out.println(queue.peek()); // bbb
System.out.println(queue); // [bbb, ccc]

System.out.println(queue.poll()); // bbb
System.out.println(queue); // [ccc]

System.out.println(queue.peek()); // ccc
System.out.println(queue); // [ccc]

System.out.println(queue.poll()); // ccc
System.out.println(queue); // []

System.out.println(queue.peek()); // null
System.out.println(queue); // []

E poll ()

Retrieves and removes the head of this queue, or returns null if this queue is empty.

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

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true

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

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

System.out.println(queue.poll()); // bbb
System.out.println(queue); // [ccc]

System.out.println(queue.poll()); // ccc
System.out.println(queue); // []

System.out.println(queue.poll()); // null
System.out.println(queue); // []

E poll (long timeout, TimeUnit unit)

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

Please see also : poll()

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

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

    final var queue = new LinkedBlockingQueue<String>();

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

                System.out.printf("  poll 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());

    queue.put("aaa");
    queue.put("bbb");
    queue.put("ccc");

    TimeUnit.SECONDS.sleep(8);

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

// Result
// ↓
//put values (0.002216 sec.)
//  poll ...
//  poll value = aaa (0.003794 sec.)
//  poll ...
//  poll value = bbb (0.003959 sec.)
//  poll ...
//  poll value = ccc (0.004136 sec.)
//  poll ...
//  poll value = null (5.018061 sec.)
//  poll ...
//future.cancel
//  InterruptedException!

void put (E e)

Inserts the specified element at the tail of this queue, 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 queue = new LinkedBlockingQueue<String>(3);
    System.out.printf("remainingCapacity = %d (%f sec.)%n",
            queue.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 ...");
                queue.put(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("queue = %s (%f sec.)%n", queue, elapsedTime.getAsDouble());

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

// Result
// ↓
//remainingCapacity = 3 (0.001505 sec.)
//  put ...
//  put OK! (0.004062 sec.)
//  put ...
//  put OK! (0.004170 sec.)
//  put ...
//  put OK! (0.004321 sec.)
//  put ...
//queue = [aaa, bbb, ccc] (5.009167 sec.)
//future.cancel
//  InterruptedException! (5.010238 sec.)

int remainingCapacity ()

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

final var queue = new LinkedBlockingQueue<String>(3);

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

System.out.println(queue.offer("aaa")); // true

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

System.out.println(queue.offer("bbb")); // true

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

System.out.println(queue.offer("ccc")); // true

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

queue.clear();

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

boolean remove (Object o)

Removes a single instance of the specified element from this queue, if it is present.

final var queue = new LinkedBlockingQueue<String>();

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("BBB")); // true
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("BBB")); // true
System.out.println(queue.offer("ccc")); // true

System.out.println(queue); // [aaa, BBB, aaa, BBB, ccc]

System.out.println(queue.remove("aaa")); // true
System.out.println(queue); // [BBB, aaa, BBB, ccc]

System.out.println(queue.remove("BBB")); // true
System.out.println(queue); // [aaa, BBB, ccc]

System.out.println(queue.remove("BBB")); // true
System.out.println(queue); // [aaa, ccc]

System.out.println(queue.remove("XXX")); // false
System.out.println(queue); // [aaa, ccc]

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 queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.removeAll(List.of())); // false
    System.out.println(queue); // [a, b, a, b, A, B]
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.removeAll(List.of("a"))); // true
    System.out.println(queue); // [b, b, A, B]
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.removeAll(List.of("a", "b"))); // true
    System.out.println(queue); // [A, B]
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.removeAll(List.of("b", "a"))); // true
    System.out.println(queue); // [A, B]
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.removeAll(List.of("A"))); // true
    System.out.println(queue); // [a, b, a, b, B]
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.removeAll(List.of("X", "Y", "Z"))); // false
    System.out.println(queue); // [a, b, a, b, A, B]
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.removeAll(List.of("A", "X", "Y", "Z"))); // true
    System.out.println(queue); // [a, b, a, b, B]
}

boolean removeIf (Predicate<? super E> filter)

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

final var queue = new LinkedBlockingQueue<String>();

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("BBB")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue.offer("DDD")); // true

System.out.println(queue); // [aaa, BBB, ccc, DDD]

final var ret = queue.removeIf(s -> {
    return s.equals(s.toUpperCase());
});

System.out.println(ret); // true
System.out.println(queue); // [aaa, ccc]
final var queue = new LinkedBlockingQueue<String>();

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true

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

final var ret = queue.removeIf(s -> s.equals(s.toUpperCase()));

System.out.println(ret); // false
System.out.println(queue); // [aaa, bbb]

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 queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.retainAll(List.of())); // true
    System.out.println(queue); // []
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.retainAll(List.of("a", "b"))); // true
    System.out.println(queue); // [a, b, a, b]
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.retainAll(List.of("b", "a"))); // true
    System.out.println(queue); // [a, b, a, b]
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.retainAll(List.of("A"))); // true
    System.out.println(queue); // [A]
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.retainAll(List.of("X", "Y", "Z"))); // true
    System.out.println(queue); // []
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.retainAll(List.of("A", "X", "Y", "Z"))); // true
    System.out.println(queue); // [A]
}
{
    final var queue = new LinkedBlockingQueue<>(src);
    System.out.println(queue); // [a, b, a, b, A, B]

    System.out.println(queue.retainAll(List.of("a", "b", "A", "B"))); // false
    System.out.println(queue); // [a, b, a, b, A, B]
}

int size ()

Returns the number of elements in this queue.

final var queue = new LinkedBlockingQueue<String>();

System.out.println(queue); // []
System.out.println(queue.size()); // 0

System.out.println(queue.offer("aaa")); // true

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

System.out.println(queue.offer("bbb")); // true

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

System.out.println(queue.offer("ccc")); // true

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

queue.clear();

System.out.println(queue); // []
System.out.println(queue.size()); // 0

Spliterator<E> spliterator ()

Returns a Spliterator over the elements in this queue.

final var queue = new LinkedBlockingQueue<String>();

System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true

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

final var spliterator = queue.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 this queue, 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 queue = new LinkedBlockingQueue<String>();

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

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

    queue.put("aaa");
    queue.put("bbb");
    queue.put("ccc");

    TimeUnit.SECONDS.sleep(5);

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

    queue.put("XXX");
    queue.put("YYY");
    queue.put("ZZZ");

    TimeUnit.SECONDS.sleep(5);

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

// Result
// ↓
//put values (0.002790 sec.)
//  take ...
//  take OK! : value = aaa (0.006541 sec.)
//  take ...
//  take OK! : value = bbb (0.006858 sec.)
//  take ...
//  take OK! : value = ccc (0.007065 sec.)
//  take ...
//put values (5.017256 sec.)
//  take OK! : value = XXX (5.017574 sec.)
//  take ...
//  take OK! : value = YYY (5.017692 sec.)
//  take ...
//  take OK! : value = ZZZ (5.017783 sec.)
//  take ...
//future.cancel
//  InterruptedException!

Object[] toArray ()

Returns an array containing all of the elements in this queue, in proper sequence.

final var queue = new LinkedBlockingQueue<String>();

System.out.println(queue.offer("a")); // true
System.out.println(queue.offer("b")); // true
System.out.println(queue.offer("c")); // true

System.out.println(queue); // [a, b, c]

final Object[] array = queue.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 queue, in proper sequence; the runtime type of the returned array is that of the specified array.

final var queue = new LinkedBlockingQueue<String>();

System.out.println(queue.offer("a")); // true
System.out.println(queue.offer("b")); // true
System.out.println(queue.offer("c")); // true

System.out.println(queue); // [a, b, c]

final String[] array = queue.toArray(new String[0]);
System.out.println(Arrays.toString(array)); // [a, b, c]
final var queue = new LinkedBlockingQueue<String>();

System.out.println(queue.offer("a")); // true
System.out.println(queue.offer("b")); // true
System.out.println(queue.offer("c")); // true

System.out.println(queue); // [a, b, c]

{
    final String[] array = new String[3];
    System.out.println(Arrays.toString(array)); // [null, null, null]

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

Methods declared in AbstractQueue

add, addAll, element, remove

Please see the link below.

Methods declared in AbstractCollection

containsAll, isEmpty, toString

Please see the link below.

Methods declared in BlockingQueue

add

Please see the link below.

Methods declared in Collection

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

Please see the link below.

Methods declared in Queue

element, remove

Please see the link below.


Related posts

To top of page