import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String[] args) { Queuequeue = new LinkedList<>(); queue.add("apple"); queue.add("banana"); queue.add("orange"); String head = queue.peek(); System.out.println(head); // outputs "apple" } }
import java.util.ArrayDeque; import java.util.Queue; public class QueueExample { public static void main(String[] args) { QueueThis code creates a queue of integers using the ArrayDeque implementation of the Queue interface. The peek() method is called to retrieve the head of the queue, but since the queue is empty, null is returned. This value is printed to the console. The java.util.Queue interface and its associated methods are part of the Java standard library.queue = new ArrayDeque<>(); Integer head = queue.peek(); System.out.println(head); // outputs "null" } }