Java Utility Library

Java ArrayDeque - remove() Method



The java.util.ArrayDeque.remove() method is used to remove the first occurrence of the specified element from this deque, if it is present. Every removal of element results into reducing the deque size by one unless the deque is empty.

Syntax

public boolean remove(Object obj)

Parameters

obj Specify the element which need to be removed from this deque, if present.

Return Value

Returns true if this deque contained the specified element.

Exception

NA.

Example:

In the example below, the java.util.ArrayDeque.remove() method is used to remove the first occurrence of 20 from the given deque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating ArrayDeque
    MyDeque.add(10);
    MyDeque.add(20);
    MyDeque.add(30);
    MyDeque.add(20);
    MyDeque.add(20);

    //printing ArrayDeque
    System.out.println("MyDeque contains: " + MyDeque);

    //remove the first occurrence of 20
    MyDeque.remove(20);

    //printing ArrayDeque
    System.out.println("MyDeque contains: " + MyDeque);
  }
}

The output of the above code will be:

MyDeque contains: [10, 20, 30, 20, 20]
MyDeque contains: [10, 30, 20, 20]

❮ Java.util - ArrayDeque