ConcurrentLinkedQueue peek() method in Java Last Updated : 26 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The peek() method of ConcurrentLinkedQueue is used to return the head of the ConcurrentLinkedQueue. It retrieves but does not remove, the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method returns null. Syntax: public E peek() Returns: This method returns the head of this ConcurrentLinkedQueue without removing it. Below programs illustrate peek() method of ConcurrentLinkedQueue: Example 1: Java // Java Program Demonstrate peek() // method of ConcurrentLinkedQueue import java.util.concurrent.*; public class GFG { public static void main(String[] args) { // create an ConcurrentLinkedQueue ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>(); // Add Numbers to queue queue.add(4353); queue.add(7824); queue.add(78249); queue.add(8724); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); // find peek int response1 = queue.peek(); // print after applying peek method System.out.println("Head: " + response1); // Verifying that the head is not removed System.out.println("ConcurrentLinkedQueue after peek: " + queue); } } Output: ConcurrentLinkedQueue: [4353, 7824, 78249, 8724] Head: 4353 ConcurrentLinkedQueue after peek: [4353, 7824, 78249, 8724] Example 2: Java // Java Program Demonstrate peek() // method of ConcurrentLinkedQueue import java.util.concurrent.*; public class GFG { public static void main(String[] args) { // create an ConcurrentLinkedQueue ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>(); // Add String to queue queue.add("Aman"); queue.add("Amar"); queue.add("Sanjeet"); queue.add("Rabi"); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); // find peek of queue String response1 = queue.peek(); // print after applying peek method System.out.println("Head: " + response1); // Verifying that the head is not removed System.out.println("ConcurrentLinkedQueue after peek: " + queue); // remove some elements queue.poll(); queue.poll(); // Displaying the existing ConcurrentLinkedQueue System.out.println("Updated ConcurrentLinkedQueue: " + queue); // find peek of queue String response2 = queue.peek(); // print after applying peek method System.out.println("Head: " + response1); // Verifying that the head is not removed System.out.println("ConcurrentLinkedQueue after peek: " + queue); } } Output: ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi] Head: Aman ConcurrentLinkedQueue after peek: [Aman, Amar, Sanjeet, Rabi] Updated ConcurrentLinkedQueue: [Sanjeet, Rabi] Head: Aman ConcurrentLinkedQueue after peek: [Sanjeet, Rabi] Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#peek-- Comment More infoAdvertise with us Next Article ConcurrentLinkedQueue size() method in Java A AmanSingh2210 Follow Improve Article Tags : Java Java-Collections Java - util package java-basics Java-Functions java-queue Java-ConcurrentLinkedQueue +3 More Practice Tags : JavaJava-Collections Similar Reads ConcurrentLinkedQueue poll() method in Java The poll() method of ConcurrentLinkedQueue is used to remove and return the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method will return null. Syntax: public E poll() Returns: This method remove and returns the head of this ConcurrentLinkedQueue or null if t 2 min read ConcurrentLinkedQueue size() method in Java The size() method of ConcurrentLinkedQueue is used to returns number of elements that this ConcurrentLinkedQueue contains. Syntax: public int size() Returns: This method number of elements in this ConcurrentLinkedQueue. Below programs illustrate size() method of ConcurrentLinkedQueue: Example 1: Jav 2 min read ConcurrentLinkedQueue offer() method in Java The offer() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method offer() will never returns false. Syntax: public boolean 2 min read ConcurrentLinkedDeque peekLast() method in Java The java.util.concurrent.ConcurrentLinkedDeque.peekLast() is an in-built method in Java which retrieves the last element of this deque container without removing it. The function returns NULL if the deque is empty. Syntax: Conn_Linked_Deque.peekLast() Parameters: The function does not accepts any pa 2 min read ConcurrentLinkedDeque peekFirst() method in Java The java.util.concurrent.ConcurrentLinkedDeque.peekFirst() is an in-built method in Java which retrieves the first element of this deque without removing it. The function returns NULL if the deque is empty. Syntax: Conn_Linked_Deque.peekFirst() Parameters: This function does not accepts any paramete 2 min read ConcurrentLinkedQueue add() method in Java The add() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter to add() of ConcurrentLinkedQueue, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method will never throw IllegalS 2 min read Like