Tuesday, June 16, 2026
HomeLanguagesJavaConcurrentLinkedQueue peek() method in Java

ConcurrentLinkedQueue peek() method in Java

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 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 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–

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS