Using Queue in Java

In Java, a Queue is a part of the Java Collections Framework. It is an interface representing a collection of elements to be processed in a First-In-First-Out (FIFO) order. The Queue interface provides several methods like add(), which adds an element to the end of the queue, remove(), which removes and returns the head of the queue, peek(), which looks at the head of the queue without removing it, poll(), which retrieves and removes the head of the queue, returning null if the queue is empty, and several others. One of the most common implementations of the Queue interface is the LinkedList, although other classes like PriorityQueue can also be used. Remember, the Queue interface doesn't support random access to its elements. Its primary goal is to provide a collection of elements to be processed sequentially. In this Java Queue Example, a queue object has been created and filled with data. Click Execute to run the Java Queue Example online and see the result.
Using Queue in Java Execute
import java.util.LinkedList;
import java.util.Queue;

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

        queue.add(1);
        queue.add(2);
        queue.add(3);

        System.out.println("Queue: " + queue);
    }
}
Updated: Viewed: 50 times