The Wayback Machine - https://web.archive.org/web/20240618020209/https://www.geeksforgeeks.org/arraydeque-addfirst-method-in-java/
Open In App

ArrayDeque addFirst() Method in Java

Last Updated : 10 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.ArrayDeque.addFirst(Object element) method in Java is used to insert a specific element at the front of this deque.

Syntax:

Array_Deque.addFirst(Object element)

Parameters: The parameter element is of the type ArrayDeque and refers to the element to be added.

Return Value: The function does not return any value.

Exceptions: The method throws NullPointerException if the passed parameter is NULL.

Below programs illustrate the Java.util.ArrayDeque.addFirst() method:
Program 1:




// Java code to illustrate addFirst()
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("ArrayDeque: " + de_que);
  
        // Adding elements at front
        de_que.addFirst(40);
        de_que.addFirst(50);
        de_que.addFirst(60);
        de_que.addFirst(70);
  
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque_front_addition: " + de_que);
  
        // Adding elements using add()
        de_que.add(1);
        de_que.add(2);
        de_que.add(3);
  
        // Displaying the final ArrayDeque
        System.out.println("Final ArrayDeque: " + de_que);
    }
}


Output:

ArrayDeque: [10, 15, 30, 20, 5]
ArrayDeque_front_addition: [70, 60, 50, 40, 10, 15, 30, 20, 5]
Final ArrayDeque: [70, 60, 50, 40, 10, 15, 30, 20, 5, 1, 2, 3]

Program 2:




// Java code to illustrate addFirst()
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("4");
        de_que.add("Geeks");
  
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque: " + de_que);
  
        // Adding elements at front
        de_que.addFirst("GFG");
        de_que.addFirst("World");
        de_que.addFirst("Hello");
  
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque_front_addition: " + de_que);
  
        // Adding elements using add()
        de_que.add("Coding");
        de_que.add("At");
        de_que.add("BEST");
  
        // Displaying the final ArrayDeque
        System.out.println("Final ArrayDeque: " + de_que);
    }
}


Output:

ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
ArrayDeque_front_addition: [Hello, World, GFG, Welcome, To, Geeks, 4, Geeks]
Final ArrayDeque: [Hello, World, GFG, Welcome, To, Geeks, 4, Geeks, Coding, At, BEST]


Previous Article
Next Article

Similar Reads

LinkedList addFirst() Method in Java
The java.util.LinkedList.addFirst() method in Java is used to insert a specific element at the beginning of a LinkedList. Syntax: void addFirst(Object element) Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended at beginning of the list. Return Value: This me
1 min read
LinkedBlockingDeque addFirst() method in Java
The addFirst(E e) method of LinkedBlockingDeque inserts the element passed in the parameter to the front of the Deque if there is space. If the LinkedBlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. Syntax: public void addFirst(E e) Parameters: This method accepts a mandatory parameter e
2 min read
ConcurrentLinkedDeque addFirst() method in Java
The java.util.concurrent.ConcurrentLinkedDeque.addFirst() is an in-built function in Java which inserts the specified element at the front of the ConcurrentLinkedDeque. Syntax: conn_linked_deque.addFirst(elem) Parameter: The method accepts only a single parameter elem which is to be added to the beginning of the ConcurrentLinkedDeque. Return Value:
2 min read
Deque addFirst() method in Java with Examples
The addFirst(E e) method of Deque Interface inserts the element passed in the parameter to the front of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax: void addFirst(E e) Parameters: This method accept
4 min read
BlockingDeque addFirst() method in Java with Examples
The addFirst(E e) method of BlockingDeque inserts the element passed in the parameter to the front of the Deque if there is space. If the BlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. Syntax: public void addFirst(E e) Parameters: This method accepts a mandatory parameter e which is the
2 min read
Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition che
3 min read
Java.util.ArrayDeque Class in Java | Set 1
java.util.ArrayDeque class describes an implementation of a resizable array structure which implements the Deque interface. Array deques has not immutable and can grow as necessary. They are not thread-safe and hence concurrent access by multiple threads is not supported unless explicitly synchronized. Null elements are invalid in this structure. M
5 min read
Java.util.ArrayDeque Class in Java | Set 2
Java.util.ArrayDeque Class in Java | Set 1 More Methods of util.ArrayDeque Class : 16. offer(Element e) : java.util.ArrayDeque.offer(Element e) : inserts element at the end of deque. Syntax : public boolean offer(Element e) Parameters : e - element to add Return : true, if element is added else; false 17. offerFirst(Element e) : java.util.ArrayDequ
5 min read
ArrayDeque offerLast() Method in Java
The Java.util.ArrayDeque.offerLast(Object element) method in Java is used to add a specific element at the end of this Deque. The function is similar to the addLast(), add() and offer() method of ArrayDeque in Java. Syntax: Array_Deque.offerLast(Object element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to
2 min read
ArrayDeque toArray() Method in Java
The java.util.ArrayDeque.toArray() method is used to form an array of the same elements as that of the Deque. Basically, the method copies all the element from this deque to a new array. Syntax: Object[] arr = Array_Deque.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the elements
2 min read
three90RightbarBannerImg