ConcurrentLinkedDeque removeFirst() method in Java
Last Updated :
18 Sep, 2018
The ConcurrentLinkedDeque.removeFirst() is an in-built function in Java which removes the first element from the deque container. The function throws a NoSuchElementException if this deque is empty.
Syntax:
Conn_Linked_Deque.removeFirst()
Parameters: The function does not accepts any parameter.
Return Values: The function returns the first element in the deque.
Exception: The function throws a NoSuchElementException if this deque is empty.
Below program illustrates the removeFirst() method:
Program 1: This program involves deque with Integer elements.
import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
public static void main(String[] args)
{
ConcurrentLinkedDeque<Integer> cld =
new ConcurrentLinkedDeque<Integer>();
cld.addFirst( 12 );
cld.addFirst( 70 );
cld.addFirst( 1009 );
cld.addFirst( 475 );
System.out.println( "Elements in"
+ "the LinkedDeque: " + cld);
System.out.println( "Element removed : "
+ cld.peekFirst());
cld.removeFirst();
System.out.println( "Elements in"
+ "the LinkedDeque: " + cld);
}
}
|
Output:
Elements inthe LinkedDeque: [475, 1009, 70, 12]
Element removed : 475
Elements inthe LinkedDeque: [1009, 70, 12]
Program 2: This program involves deque with String elements.
import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
public static void main(String[] args)
{
ConcurrentLinkedDeque<String> cld =
new ConcurrentLinkedDeque<String>();
cld.addFirst( "GFG" );
cld.addFirst( "Gfg" );
cld.addFirst( "GeeksforGeeks" );
cld.addFirst( "Geeks" );
System.out.println( "Elements in"
+ "the LinkedDeque: " + cld);
System.out.println( "Element removed : "
+ cld.peekFirst());
cld.removeFirst();
System.out.println( "Elements in"
+ "the LinkedDeque: " + cld);
}
}
|
Output:
Elements inthe LinkedDeque: [Geeks, GeeksforGeeks, Gfg, GFG]
Element removed : Geeks
Elements inthe LinkedDeque: [GeeksforGeeks, Gfg, GFG]
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#removeFirst()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...