Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

ArrayDeque remove() Method in Java

  • Difficulty Level : Basic
  • Last Updated : 10 Dec, 2018
  1. The Java.util.ArrayDeque.remove() method is used to remove the element present at the head of the Deque.

    Syntax:

    Array_Deque.remove()

    Parameters: The method does not take any parameters.

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

    Return Value: This method returns the element present at the head of the Deque.

    Exceptions: The method throws NoSuchElementException is thrown if the deque is empty.



    Below programs illustrate the Java.util.ArrayDeque.remove() method:

    Program 1:




    // Java code to illustrate remove()
    import java.util.*;
      
    public class ArrayDequeDemo {
        public static void main(String args[])
        {
            // Creating an empty ArrayDeque
            Deque<String> de_que = new ArrayDeque<String>();
      
            // Use add() method to add elements into the Deque
            de_que.add("Welcome");
            de_que.add("To");
            de_que.add("Geeks");
            de_que.add("For");
            de_que.add("Geeks");
      
            // Displaying the ArrayDeque
            System.out.println("Initial ArrayDeque: " + de_que);
      
            // Removing elements using remove() method
            de_que.remove();
            de_que.remove();
      
            // Displaying the ArrayDeque after removal
            System.out.println("ArrayDeque after removing "
                               + "elements: " + de_que);
        }
    }
    Output:
    Initial ArrayDeque: [Welcome, To, Geeks, For, Geeks]
    ArrayDeque after removing elements: [Geeks, For, Geeks]
    

    Program 2:




    // Java code to illustrate remove()
    import java.util.*;
      
    public class ArrayDequeDemo {
        public static void main(String args[])
        {
            // Creating an empty ArrayDeque
            Deque<Integer> de_que = new ArrayDeque<Integer>();
      
            // Use add() method to add elements into the Deque
            de_que.add(10);
            de_que.add(15);
            de_que.add(30);
            de_que.add(20);
            de_que.add(5);
      
            // Displaying the ArrayDeque
            System.out.println("Initial ArrayDeque: " + de_que);
      
            // Removing elements using remove() method
            de_que.remove();
            de_que.remove();
      
            // Displaying the ArrayDeque after removal
            System.out.println("ArrayDeque after removing "
                               + "elements: " + de_que);
        }
    }
    Output:
    Initial ArrayDeque: [10, 15, 30, 20, 5]
    ArrayDeque after removing elements: [30, 20, 5]
    
  2. The Java.util.ArrayDeque.remove(Object) method is used to remove a particular element from an ArrayDeque.

    Syntax:

    Priority_Queue.remove(Object O)

    Parameters: The parameter O is of the type of ArrayDeque and specifies the element to be removed from the ArrayDeque.

    Return Value: This method returns True if the specified element is present in the Deque else it returns False.

    Below programs illustrate the Java.util.ArrayDeque.remove() method:

    Program 1:




    // Java code to illustrate remove()
    import java.util.*;
      
    public class ArrayDequeDemo {
        public static void main(String args[])
        {
            // Creating an empty ArrayDeque
            Deque<String> de_que = new ArrayDeque<String>();
      
            // Use add() method to add elements into the Deque
            de_que.add("Welcome");
            de_que.add("To");
            de_que.add("Geeks");
            de_que.add("For");
            de_que.add("Geeks");
      
            // Displaying the ArrayDeque
            System.out.println("Initial ArrayDeque: " + de_que);
      
            // Removing elements using remove() method
            de_que.remove("Geeks");
            de_que.remove("For");
            de_que.remove("Welcome");
      
            // Displaying the ArrayDeque after removal
            System.out.println("ArrayDeque after removing "
                               + "elements: " + de_que);
        }
    }
    Output:
    Initial ArrayDeque: [Welcome, To, Geeks, For, Geeks]
    ArrayDeque after removing elements: [To, Geeks]
    

    Program 2:




    // Java code to illustrate remove()
    import java.util.*;
      
    public class ArrayDequeDemo {
        public static void main(String args[])
        {
            // Creating an empty ArrayDeque
            Deque<Integer> de_que = new ArrayDeque<Integer>();
      
            // Use add() method to add elements into the Deque
            de_que.add(10);
            de_que.add(15);
            de_que.add(30);
            de_que.add(20);
            de_que.add(5);
      
            // Displaying the ArrayDeque
            System.out.println("Initial ArrayDeque: " + de_que);
      
            // Removing elements using remove() method
            de_que.remove(30);
            de_que.remove(5);
      
            // Displaying the ArrayDeque after removal
            System.out.println("ArrayDeque after removing "
                               + "elements: " + de_que);
        }
    }
    Output:
    Initial ArrayDeque: [10, 15, 30, 20, 5]
    ArrayDeque after removing elements: [10, 15, 20]
    



My Personal Notes arrow_drop_up
Recommended Articles
Page :