An Deque
interface. ArrayDeque
can be used as both Stack or Queue. Null
elements are prohibited.
removeFirst
methodThe removeFirst
method gets and removes the first element of the ArrayDeque
object.
public E removeFirst()
This method doesn’t take any parameters.
This method retrieves and removes the first element of the Deque
object. If the Deque
object is empty then NoSuchElementException
is thrown.
This method is the same as the
pollFirst
method except that thepollFirst
method doesn’t throwNoSuchElementException
if theDeque
is empty.
The code below demonstrates the use of the removeFirst
method:
import java.util.ArrayDeque;class RemoveFirst {public static void main( String args[] ) {ArrayDeque<String> arrayDeque = new ArrayDeque<>();arrayDeque.add("1");arrayDeque.add("2");arrayDeque.add("3");System.out.println("The arrayDeque is " + arrayDeque);System.out.println("arrayDeque.removeFirst() returns : " + arrayDeque.removeFirst());System.out.println("The arrayDeque is " + arrayDeque);}}
In the code above:
In line 1, we import the ArrayDeque
class.
In line 4, we create a ArrayDeque
object named arrayDeque
.
From line 5 to 7, we use the arrayDeque
object to add three elements("1"
,"2"
,"3"
) to Deque
.
In line 10, we use the removeFirst()
method of the arrayDeque
object to get and remove the first element. In our case, 1
will be removed and returned.