Java Deque addFirst() Method

The addFirst() method of Java Deque Interface is used to insert the specified element at the front of the deque .

Syntax:

Parameter:

The above method consists of only one parameter:

  1. The element 'e' that needs to be added.

Return:

NA

Throw:

IllegalStateException- If the elements cannot be added at the time due to capacity restriction.

ClassCastException- If the class of the specified element avoids it from being added to the deque.

NullPointerException- If the specified element is a null and the deque does not allow the null elements.

IllegalArgumentException- If some of the property of the given element avoids it from being added to the deque.

Example 1

Test it Now

Output:

The elements are adding at the front of the deque : [11, 22, 33, 44]

Example 2

Output:

The subjects are given as : [DBMS, JAVA, CAO, FAT]

Example 3

Output:

Enter the number of times you want to enter the names : 4
The names are : 
Sham
Ram
Stuti
Ranajna
The final deque is : [Ranajna, Stuti, Ram, Sham]
Next TopicJava Deque




Latest Courses