PriorityQueue peek() Method in Java Last Updated : 10 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The java.util.PriorityQueue.peek() method in Java is used to retrieve or fetch the first element of the Queue or the element present at the head of the Queue. The element retrieved does not get deleted or removed from the Queue. Syntax: Priority_Queue.peek() Parameters: The method does not take any parameters. Return Value: The method returns the element at the head of the Queue else returns NULL if the Queue is empty. Below programs illustrate the java.util.PriorityQueue.peek() method: Program 1: Java // Java code to illustrate peek() import java.util.*; public class PriorityQueueDemo { public static void main(String args[]) { // Creating an empty PriorityQueue PriorityQueue<String> queue = new PriorityQueue<String>(); // Use add() method to add elements into the Queue queue.add("Welcome"); queue.add("To"); queue.add("Geeks"); queue.add("For"); queue.add("Geeks"); // Displaying the PriorityQueue System.out.println("Initial PriorityQueue: " + queue); // Fetching the element at the head of the queue System.out.println("The element at the head of the" + " queue is: " + queue.peek()); // Displaying the Queue after the Operation System.out.println("Final PriorityQueue: " + queue); } } Output: Initial PriorityQueue: [For, Geeks, To, Welcome, Geeks] The element at the head of the queue is: For Final PriorityQueue: [For, Geeks, To, Welcome, Geeks] Program 2: Java // Java code to illustrate peek() import java.util.*; public class PriorityQueueDemo { public static void main(String args[]) { // Creating an empty PriorityQueue PriorityQueue<Integer> queue = new PriorityQueue<Integer>(); // Use add() method to add elements into the Queue queue.add(10); queue.add(15); queue.add(30); queue.add(20); queue.add(5); // Displaying the PriorityQueue System.out.println("Initial PriorityQueue: " + queue); // Fetching the element at the head of the queue System.out.println("The element at the head of the" + " queue is: " + queue.peek()); // Displaying the Queue after the Operation System.out.println("Final PriorityQueue: " + queue); } } Output: Initial PriorityQueue: [5, 10, 30, 20, 15] The element at the head of the queue is: 5 Final PriorityQueue: [5, 10, 30, 20, 15] Comment More infoAdvertise with us Next Article PriorityQueue peek() Method in Java C chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-priority-queue +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads PriorityQueue poll() Method in Java The java.util.PriorityQueue.poll() method in Java is used to retrieve or fetch and remove the first element of the Queue or the element present at the head of the Queue. The peek() method only retrieved the element at the head but the poll() also removes the element along with the retrieval. It retu 2 min read PriorityQueue offer() Method in Java The java.util.PriorityQueue.offer() method is used to insert a particular element into the Priority Queue. It acts similar to the add() method of Priority Queue. Syntax: Priority_Queue.offer(Object element) Parameters: The parameter element is of the type PriorityQueue and refers to the element to b 2 min read PriorityBlockingQueue peek() method in Java The peek() method of PriorityBlockingQueue returns the element at head of the PriorityBlockingQueue. It retrieves the value of the head of LinkedBlockingQueue but does not remove it. If the PriorityBlockingQueue does not contains any element, then this method returns null. A PriorityBlockingQueue qu 2 min read Queue peek() method in Java The peek() method of Queue Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the queue. The method does not throws an exception when the Queue is empty, it returns null instead. Syntax: E peek() Returns: This m 3 min read PriorityQueue add() Method in Java The Java.util.PriorityQueue.add() method in Java is used to add a specific element into a PriorityQueue. This method internally just calls the Java.util.PriorityQueue.offer() method with the value passed to it. So, it exactly works like offer() method. Syntax: Priority_Queue.add(Object element) Para 2 min read Like