import java.util.Queue; import java.util.LinkedList; public class QueueExample { public static void main(String[] args) { Queuequeue = new LinkedList<>(); // adding elements to the queue queue.offer("One"); queue.offer("Two"); queue.offer("Three"); System.out.println(queue); } }
import java.util.Queue; import java.util.LinkedList; public class QueueExample { public static void main(String[] args) { QueueOutput: One Two Three In this example, we use the poll() method to retrieve elements from the queue in a FIFO (First In, First Out) manner until the queue is empty. The java.util Queue is a standard package library in Java 8 and does not require any additional imports or installations.queue = new LinkedList<>(); queue.offer("One"); queue.offer("Two"); queue.offer("Three"); // polling elements from the queue while (!queue.isEmpty()) { System.out.println(queue.poll()); } } }