The java.util.ConcurrentLinkedDeque.peek() method in Java is used to retrieve or fetch the element at the head of the Deque. The element retrieved does not get deleted or removed from the Deque instead the method just returns it. If no element is present in the deque then Null is returned.
Syntax:
Array_Deque.peek()
Parameters: The method does not take any parameter.
Return Value: The method returns the element at the head of the Deque.
Below programs illustrate the Java.util.ConcurrentLinkedDeque.peek() method:
Program 1:
import java.util.concurrent.*;
public class ConcurrentLinkedDequeDemo {
public static void main(String args[])
{
ConcurrentLinkedDeque<String> de_que
= new ConcurrentLinkedDeque<String>();
de_que.add( "Welcome" );
de_que.add( "To" );
de_que.add( "Geeks" );
de_que.add( "4" );
de_que.add( "Geeks" );
System.out.println( "Initial ConcurrentLinkedDeque: "
+ de_que);
System.out.println( "The element at head is: "
+ de_que.peek());
System.out.println( "Final ConcurrentLinkedDeque: "
+ de_que);
}
}
|
Output:
Initial ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
The element at head is: Welcome
Final ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
Program 2:
import java.util.concurrent.*;
public class ConcurrentLinkedDequeDemo {
public static void main(String args[])
{
ConcurrentLinkedDeque<Integer> de_que
= new ConcurrentLinkedDeque<Integer>();
de_que.add( 10 );
de_que.add( 15 );
de_que.add( 30 );
de_que.add( 20 );
de_que.add( 5 );
System.out.println( "Initial ConcurrentLinkedDeque: "
+ de_que);
System.out.println( "The element at head is: "
+ de_que.peek());
System.out.println( "Final ConcurrentLinkedDeque: "
+ de_que);
}
}
|
Output:
Initial ConcurrentLinkedDeque: [10, 15, 30, 20, 5]
The element at head is: 10
Final ConcurrentLinkedDeque: [10, 15, 30, 20, 5]
Program 3: For an empty deque:
import java.util.concurrent.*;
public class ConcurrentLinkedDequeDemo {
public static void main(String args[])
{
ConcurrentLinkedDeque<Integer> de_que
= new ConcurrentLinkedDeque<Integer>();
System.out.println( "ConcurrentLinkedDeque: "
+ de_que);
System.out.println( "The element at head is: "
+ de_que.peek());
}
}
|
Output:
ConcurrentLinkedDeque: []
The element at head is: null
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!