import java.util.LinkedList; import java.util.Queue; public class Example1 { public static void main(String[] args) { Queuequeue = new LinkedList<>(); // add elements queue.add("A"); queue.add("B"); queue.add("C"); // remove head element String head = queue.remove(); System.out.println("Head element: " + head); } }
import java.util.ArrayDeque; import java.util.Queue; public class Example2 { public static void main(String[] args) { QueueThis code outputs: "Head element: 1". Here, we're using the ArrayDeque class from the java.util package to implement a queue.queue = new ArrayDeque<>(); // add elements queue.add(1); queue.add(2); queue.add(3); // remove head element int head = queue.remove(); System.out.println("Head element: " + head); } }