Java : AbstractQueue with Examples

AbstractQueue (Java SE 20 & JDK 20) API Examples.
You will find code examples on most AbstractQueue<E> methods.


Summary

This class provides skeletal implementations of some Queue operations. The implementations in this class are appropriate when the base implementation does not allow null elements. Methods add, remove, and element are based on offer, poll, and peek, respectively, but throw exceptions instead of indicating failure via false or null returns.

Class diagram

class MyQueue extends AbstractQueue<String> {
    private final List<String> values = new ArrayList<>();

    @Override
    public Iterator<String> iterator() {
        return values.iterator();
    }

    @Override
    public int size() {
        return values.size();
    }

    @Override
    public boolean offer(String s) {
        Objects.requireNonNull(s);
        return values.add(s);
    }

    @Override
    public String poll() {
        return values.isEmpty() ? null : values.remove(0);
    }

    @Override
    public String peek() {
        return values.isEmpty() ? null : values.get(0);
    }
}

Note : The MyQueue class uses ArrayList for implementation, but it may not be efficient.

final var queue = new MyQueue();
queue.offer("aaa");
queue.offer("bbb");
queue.offer("ccc");

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

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

Constructors

AbstractQueue ()

Constructor for use by subclasses.

class MyQueue extends AbstractQueue<String> {
    private final List<String> values = new ArrayList<>();

    @Override
    public Iterator<String> iterator() {
        return values.iterator();
    }

    @Override
    public int size() {
        return values.size();
    }

    @Override
    public boolean offer(String s) {
        Objects.requireNonNull(s);
        return values.add(s);
    }

    @Override
    public String poll() {
        return values.isEmpty() ? null : values.remove(0);
    }

    @Override
    public String peek() {
        return values.isEmpty() ? null : values.get(0);
    }
}
final var queue = new MyQueue();
queue.offer("aaa");
queue.offer("bbb");
queue.offer("ccc");

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

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

Methods

boolean add (E e)

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

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

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

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

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

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

// Result
// ↓
//IllegalStateException! : Queue full

boolean addAll (Collection<? extends E> c)

Adds all of the elements in the specified collection to this queue.

final AbstractQueue<String> queue = new ArrayBlockingQueue<>(10);
System.out.println(queue); // []

System.out.println(queue.addAll(List.of("a1"))); // true
System.out.println(queue); // [a1]

System.out.println(queue.addAll(List.of("b1", "b2"))); // true
System.out.println(queue); // [a1, b1, b2]

System.out.println(queue.addAll(List.of("c1", "c2", "c3"))); // true
System.out.println(queue); // [a1, b1, b2, c1, c2, c3]

void clear ()

Removes all of the elements from this queue.

final AbstractQueue<String> queue = new ArrayBlockingQueue<>(10);
System.out.println(queue); // []

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

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

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

E element ()

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

final AbstractQueue<String> queue = new ArrayBlockingQueue<>(10);
System.out.println(queue); // []

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

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

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

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

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

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

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

try {
    queue.element();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

E remove ()

Retrieves and removes the head of this queue.

final AbstractQueue<String> queue = new ArrayBlockingQueue<>(10);
System.out.println(queue); // []

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

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

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

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

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

try {
    queue.remove();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

Methods declared in AbstractCollection

contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, toArray, toArray, toString

Please see the link below.

Methods declared in Collection

contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArray

Please see the link below.

Methods declared in Iterable

forEach

Please see the link below.

Methods declared in Queue

offer, peek, poll

Please see the link below.


Related posts

To top of page