Wednesday, July 3, 2024
HomeLanguagesJavaLinkedTransferQueue peek() method in Java

LinkedTransferQueue peek() method in Java

The java.util.concurrent.LinkedTransferQueue.peek() method is an in-built function in Java which is used to return the head of the queue if the queue is non-empty.

Syntax:

LinkedTransferQueue.peek()  

Parameters: The function does not accept any parameter.

Return Value: The function returns the head of the queue if the queue is non-empty, otherwise it returns null.

Below programs illustrate the LinkedTransferQueue.peek() method:

Program 1:




// Java Program Demonstrate peek()
// method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueuePeekExample1 {
    public static void main(String[] args)
    {
        // Initializing the queue
        LinkedTransferQueue<Character> queue = 
                      new LinkedTransferQueue<Character>();
  
        // Adding elements to this queue
        for (char ch = 'A'; ch <= 'Z'; ch++) {
            queue.add(ch);
        }
  
        // Printing the head of the queue
        System.out.println("The head of the queue is " 
                                        + queue.peek());
  
        // Printing all the elements of the queue
        System.out.println("The elements in the queue :");
        for (Character i : queue)
            System.out.print(i + " ");
    }
}


Output:

The head of the queue is A
The elements in the queue :
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Program 2:




// Java Program Demonstrate peek()
// method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueuePeekExample2 {
    public static void main(String[] args)
    {
        // Initializing the queue
        LinkedTransferQueue<Integer> queue = 
                 new LinkedTransferQueue<Integer>();
  
        // Adding elements to this queue
        for (int i = 10; i <= 50; i += 10)
            queue.add(i);
  
        // Printing the head of the queue
        System.out.println("The head of the queue is " 
                                           + queue.peek());
  
        // Printing all the elements of the queue
        System.out.println("The elements in the queue :");
        for (Integer i : queue)
            System.out.print(i + " ");
    }
}


Output:

The head of the queue is 10
The elements in the queue :
10 20 30 40 50

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#peek()

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments