Queue

In Java, the Queue interface is part of the java.util package and represents a collection designed for holding elements prior to processing. Queues typically order elements in a FIFO (First-In-First-Out) manner, where elements are added at the end (tail) and removed from the beginning (head).

public interface Queue<E> extends Collection<E> {
    // Methods
}Code language: PHP (php)

Methods:

  1. Adding Elements:
    • boolean add(E e): Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. Returns true upon success and throws an IllegalStateException if no space is currently available.
    • boolean offer(E e): Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. Returns true upon success and false if no space is currently available.
  2. Removing Elements:
    • E remove(): Retrieves and removes the head of this queue. Throws a NoSuchElementException if this queue is empty.
    • E poll(): Retrieves and removes the head of this queue, or returns null if this queue is empty.
  3. Inspecting Elements:
    • E element(): Retrieves, but does not remove, the head of this queue. Throws a NoSuchElementException if this queue is empty.
    • E peek(): Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
  4. Queue Size and Capacity:
    • int size(): Returns the number of elements in this queue.
    • boolean isEmpty(): Returns true if this queue contains no elements.
  5. Bulk Operations:
    • boolean contains(Object o): Returns true if this queue contains the specified element.
    • boolean containsAll(Collection<?> c): Returns true if this queue contains all of the elements of the specified collection.
  6. Conversion to Array:
    • Object[] toArray(): Returns an array containing all of the elements in this queue.
    • <T> T[] toArray(T[] a): Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.
import java.util.*;

public class QueueExample {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();

        // Adding elements to the queue
        queue.add("Element1");
        queue.offer("Element2");

        // Peeking at the head element
        System.out.println("Head: " + queue.peek());

        // Removing elements
        String removedElement = queue.remove();
        System.out.println("Removed Element: " + removedElement);

        // Iterating over elements
        for (String element : queue) {
            System.out.println("Element: " + element);
        }

        // Checking if queue contains an element
        System.out.println("Queue contains Element1: " + queue.contains("Element1"));

        // Size of the queue
        System.out.println("Size of the queue: " + queue.size());

        // Clearing the queue
        queue.clear();

        // Checking if queue is empty
        System.out.println("Queue is empty: " + queue.isEmpty());
    }
}
Scroll to Top