Java ArrayDeque Class: removeFirst() Method
public E removeFirst()
The removeFirst() method is used to retrieve and remove the first element of a given deque.
This method differs from pollFirst only in that it throws an exception if this deque is empty.
Package: java.util
Java Platform: Java SE 8
Syntax:
removeFirst()
Return Value:
the head of this deque
Return Value Type: E - the type of elements held in this collection
Throws:
NoSuchElementException - if this deque is empty
Pictorial Presentation
Example: Java ArrayDeque Class: removeFirst() Method
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
// Create an array deque
Deque deque = new ArrayDeque(8);
// Use add() method to add elements in the deque
deque.add(100);
deque.add(200);
deque.add(150);
deque.add(95);
// Print all the elements of the original deque
System.out.println("Elements of the original deque:");
for (Integer number : deque) {
System.out.println("Number = " + number);
}
// Using remove() remove element at the first postion
int re = deque.removeFirst();
System.out.println("Element removed is: " + re);
// Print all the elements available in deque after pushing two elements using removeFirst()
System.out.println("New deque:");
for (Integer number : deque) {
System.out.println("Number = " + number);
}
}
}
Output:
Elements of the original deque: Number = 100 Number = 200 Number = 150 Number = 95 Element removed is: 100 New deque: Number = 200 Number = 150 Number = 95
Java Code Editor:
Previous:remove Method
Next:removeFirstOccurrence
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_removefirst.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics