Java LinkedBlockingDeque addFirst() method

21 Mar 2025 | 2 min read

The addFirst() method of LinkedBlockingDeque class inserts the specified element at the front of this LinkedBlockingDeque. This method throws IllegalStateException if the specified element is null.

Syntax:

Parameters:

e- It is the element to add

Specified By:

The addFirst() method of ConcurrentLinkedQueue class is specified by :

  1. addFirst in interface BlockingDeque<E>
  2. addFirst in interface Deque<E>.

Throws:

NullPointerException: This exception will throw if the specified element e is null.

IllegalStateException: This exception will throw if this deque is full.

Example 1

Output:

 [1, 2, 3, 4, 5] After inserting 10 at first position [10, 1, 2, 3, 4, 5] 

Example 2

Output:

 1. Disha 2. Tanmay 3. Prachi 4. Garvit After adding string : 1. Diya 2. Disha 3. Tanmay 4. Prachi 5. Garvit 

Example 3

Output:

 Exception in thread "main" java.lang.IllegalStateException: Deque full	at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:326)	at com.javaTpoint.LinkedBlockingDequeAddFirstExample3.main	(LinkedBlockingDequeAddFirstExample3.java:13)