Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - Deque addFirst() Method



The Java Deque addFirst(E e) method inserts the specified element E at the front of the deque. This method helps in setting the the first element in the queue during the course of various add operations.

Declaration

Following is the declaration for java.util.Deque.addFirst() method −

public void addFirst(E e)

Parameters

e − The element to be added in the deque.

Return Value

This method does not return any value.

Exception

IllegalStateException − if not all the elements can be added at this time due to insertion restrictions.

ClassCastException − if the class of an element of the specified collection prevents it from being added to this deque.

NullPointerException − if the specified collection contains a null element and this deque does not permit null elements, or if the specified collection is null.

IllegalArgumentException − if some property of an element of the specified collection prevents it from being added to this deque.

Example 1

The following example shows the usage of Java Deque addFirst(E) method. In this example, we're using integers. As first, we'll add some items to the deque using add() method and then using addFirst() method, we'll add elements to the front of the deque. Then we're adding more elements using add() method again and the print the arraydeque to check if insertions in the deque are in the order we desired.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class DequeDemo {
   public static void main(String[] args) {
      
      // create an empty deque
      Deque<Integer> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(4);
      deque.add(5);
      deque.add(6);

      // use addFirst() method to add element at the front of the deque
      deque.addFirst(3);
      deque.addFirst(2);
      deque.addFirst(1);//now, element 1 will be at the front

      // these elements will be added in continuation with deque.add(6)
      deque.add(7);
      deque.add(8);

      // let us print all the elements available in deque
      System.out.println("Deque = " + deque);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Deque = [1, 2, 3, 4, 5, 6, 7, 8]

Example 2

The following example shows the usage of Java Deque addFirst(E) method. In this example, we're using strings. As first, we'll add some items to the deque using add() method and then using addFirst() method, we'll add elements to the front of the deque. Then we're adding more elements using add() method again and the print the arraydeque to check if insertions in the deque are in the order we desired.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class DequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      Deque<String> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add("D");
      deque.add("E");
      deque.add("F");

      // use addFirst() method to add element at the front of the deque
      deque.addFirst("C");
      deque.addFirst("B");
      deque.addFirst("A");//now, element A will be at the front

      // these elements will be added in continuation with deque.add("F")
      deque.add("G");
      deque.add("H");

      // let us print all the elements available in deque
      System.out.println("Deque = " + deque);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Deque = [A, B, C, D, E, F, G, H]

Example 3

The following example shows the usage of Java Deque addFirst(E) method. In this example, we're using Student objects. As first, we'll add some items to the deque using add() method and then using addFirst() method, we'll add elements to the front of the deque. Then we're adding more elements using add() method again and the print the arraydeque to check if insertions in the deque are in the order we desired.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class DequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<Student> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(new Student(4, "Julie"));
      deque.add(new Student(5, "Robert"));
      deque.add(new Student(6, "Adam"));

      // use addFirst() method to add element at the front of the deque
      deque.addFirst(new Student(3, "Rohan"));
      deque.addFirst(new Student(2, "Sohan"));
      deque.addFirst(new Student(1, "Mohan"));//now, Student 1 will be at the front

      // these elements will be added in continuation with deque.add(new Student(6, "Adam"))
      deque.add(new Student(7, "Ali"));
      deque.add(new Student(8, "Ahmad"));

      // let us print all the elements available in deque
      System.out.println("Deque = " + deque);      
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Deque = [[ 1, Mohan ], [ 2, Sohan ], [ 3, Rohan ], [ 4, Julie ], [ 5, Robert ], [ 6, Adam ], [ 7, Ali ], [ 8, Ahmad ]]
java_util_deque.htm
Advertisements