Java : Queue with Examples
Queue (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the Queue<E> methods.
Summary
A collection designed for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations.
final Queue<String> queue = new ArrayDeque<>();
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); // []
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 Queue<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
E element ()
Retrieves, but does not remove, the head of this queue.
final Queue<String> queue = new ArrayDeque<>();
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!
boolean offer (E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
// capacity = 3
final Queue<String> queue = new ArrayBlockingQueue<>(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]
E peek ()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
final Queue<String> queue = new ArrayDeque<>();
System.out.println(queue); // []
queue.add("aaa");
queue.add("bbb");
queue.add("ccc");
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 Queue<String> queue = new ArrayDeque<>();
System.out.println(queue); // []
queue.add("aaa");
queue.add("bbb");
queue.add("ccc");
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 remove ()
Retrieves and removes the head of this queue.
final Queue<String> queue = new ArrayDeque<>();
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 Collection
addAll, clear, 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
Related posts
- API Examples