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 add() Method



The Java Deque add(E e) method inserts the specified element E at the end of the deque. This method is equivalent to addLast(E). Insertion order is maintained while adding the element to the Deque and we can check the same by printing each element one by one.

Declaration

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

public boolean add(E e)

Parameters

e − The element to be added in the deque.

Return Value

This method returns true if given element is added successfully into the deque, otherwise it returns false.

Exception

IllegalStateException − if the element cannot be added at this time due to capacity restrictions.

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

NullPointerException − if the specified element is null and this deque does not permit null elements.

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

Example 1

The following example shows the usage of Java Deque add(E) method to add Integers. We're adding couple of Integers to the Deque object using add() method calls per element and then print each element to show the elements added.

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 with an initial capacity
      Deque<Integer> deque = new ArrayDeque<>(5);

      // use add() method to add elements in the deque
      deque.add(20);
      deque.add(30);
      deque.add(20);
      deque.add(30);
      deque.add(15);
      deque.add(22);
      deque.add(11);

      // let us print all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }
   }
}

Output

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

Number = 20
Number = 30
Number = 20
Number = 30
Number = 15
Number = 22
Number = 11

Example 2

The following example shows the usage of Java Deque add(E) method to add Strings. We're adding couple of strings to the Deque object using add() method calls per element and then printing the Deque using its toString() method.

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("Welcome");
      deque.add("To");
      deque.add("Tutorialspoint");
      System.out.println("Deque = " + deque);      
   }
}

Output

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

Deque = [Welcome, To, Tutorialspoint]

Example 3

The following example shows the usage of Java Deque add(E) method to add Student objects. We're adding couple of Student objects to the Deque object using add() method calls per element and then printing the Deque using its toString() method.

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(1, "Julie"));
      deque.add(new Student(2, "Robert"));
      deque.add(new Student(3, "Adam"));
      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, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_deque.htm
Advertisements