Home »
Java programming language
Java ArrayDeque remove() Method with Example
ArrayDeque Class remove() method: Here, we are going to learn about the remove() method of ArrayDeque Class with its syntax and example.
Submitted by Preeti Jain, on January 17, 2020
ArrayDeque Class remove() method
Syntax:
public boolean remove(Object obj);
public T remove();
- remove() method is available in java.lang package.
- remove() method is used to return the head element but with removing the head element from this deque.
- remove(Object obj) method is used to remove the given object from this deque.
- remove() method may throw an exception at the time of removing an element from this deque.
NoSuchElementException: This exception may throw when this deque is "blank".
- remove(Object obj) method does not throw an exception at the time of removing an element from this deque.
- These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.
Parameter(s):
- In the first case, remove(Object obj) - This parameter represents the element to be removed from the deque.
- In the second case, remove() - This method accepts none parameters.
Return value:
In the first case, the return type of this method is boolean, it returns true when the given element is removed successfully.
In the second case, the return type of this method is T, it removes the head element with returning the element of this deque.
Example:
// Java program to demonstrate the example
// of remove() method of ArrayDeque
import java.util.*;
public class RemoveOfArrayDeque {
public static void main(String[] args) {
// Creating an ArrayDeque with initial capacity of
// storing elements
Deque < String > d_queue = new ArrayDeque < String > (10);
// By using add() method to add elements
// in ArrayDeque
d_queue.add("C");
d_queue.add("C++");
d_queue.add("Java");
d_queue.add("Php");
d_queue.add("DotNet");
// Display Deque Elements
System.out.println("d_queue before remove(): ");
System.out.println("ArrayDeque Elements = " + d_queue);
// By using remove() method is to remove the
// first element from ArrayDeque
String ele = d_queue.remove();
System.out.println();
// Display Deque Elements
System.out.println("d_queue after remove() : ");
System.out.println(" d_queue.remove() = " + d_queue);
// By using remove(Object) method is to remove the
// given object from ArrayDeque
boolean b = d_queue.remove("Java");
System.out.println();
// Display Deque Elements
System.out.println("d_queue after remove(Object) : ");
System.out.println(" d_queue.remove(object) = " + d_queue);
}
}
Output
d_queue before remove():
ArrayDeque Elements = [C, C++, Java, Php, DotNet]
d_queue after remove() :
d_queue.remove() = [C++, Java, Php, DotNet]
d_queue after remove(Object) :
d_queue.remove(object) = [C++, Php, DotNet]