Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaArrayDeque addAll() method in Java

ArrayDeque addAll() method in Java

The addAll() method of ArrayDeque is used to insert all the elements of the collection passed as parameter at the end of this ArrayDeque. For adding elements of a collection to ArrayDeque we have to iterate through the collection and add each element in ArrayDeque by using addAll(E e) method. This method works same as we discussed here but with less effort. This method returns True if this deque changed as a result of the calling this method. 

Syntax:

public boolean addAll(Collection<? extends E> c)

Parameter: This method takes a parameter c which represents Collection of the elements we want to insert into this deque. 

Returns: This method returns True if this deque changed as a result of the calling this method. 

Exception: This method throws NullPointerException if the specified collection or any of its elements are null. 

Below programs illustrate addAll() method of ArrayDeque: 

Program 1: Program to demonstrate addAll() method on ArrayDeque which going add a collection containing Numbers. 

Java




// Java Program Demonstrate addAll()
// method of ArrayDeque
 
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
 
        // create an ArrayDeque which going to
        // contains a list of Numbers
        ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
 
        // Add Number to list
        Numbers.add(23);
        Numbers.add(32);
        Numbers.add(45);
        Numbers.add(63);
 
        // print ArrayDeque before calling addAll()
        System.out.println("Before calling addAll()");
        print(Numbers);
 
        // create a collection of Number to
        // add in ArrayDeque using addAll()
        ArrayList<Integer> col = new ArrayList<Integer>();
 
        // add Numbers in collection
        col.add(74);
        col.add(65);
        col.add(84);
 
        // add the elements of collection at the end
        // of ArrayDeque using addAll()
        Numbers.addAll(col);
 
        // print ArrayDeque
        System.out.println("After calling addAll()");
        print(Numbers);
    }
 
    // printing all elements of ArrayDeque
    public static void print(ArrayDeque<Integer> arDe)
    {
 
        arDe.forEach((n) -> System.out.print(n + " "));
 
        System.out.println();
    }
}


Output:

Before calling addAll()
23 32 45 63 
After calling addAll()
23 32 45 63 74 65 84

Program 2: Program to demonstrate addAll() method on ArrayDeque which going to add a collection of Students Names. 

Java




// Java Program Demonstrate addAll()
// method of ArrayDeque
 
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
        // create an ArrayDeque which going to
        // contains a list of Student names which is actually
        // string values
        ArrayDeque<String> students = new ArrayDeque<String>();
 
        // Add Strings to list
        // each string represents student name
        students.add("Ram");
        students.add("Mohan");
        students.add("Sohan");
        students.add("Rabi");
 
        // print ArrayDeque before calling addAll()
        System.out.println("Before calling addAll()");
        print(students);
 
        // create a collection of String to
        // add in ArrayDeque using addAll()
        LinkedList<String> col = new LinkedList<String>();
 
        // add Names in collection
        col.add("Rohan");
        col.add("Kartik");
 
        // add the elements of collection at the end
        // of ArrayDeque using addAll()
        students.addAll(col);
 
        // print ArrayDeque
        System.out.println("After calling addAll()");
        print(students);
    }
 
    // printing all elements of ArrayDeque
    public static void print(ArrayDeque<String> arDe)
    {
 
        System.out.println("List of Students Name:");
 
        arDe.forEach((n) -> System.out.print(" | " + n + " | "));
 
        System.out.println("\n");
    }
}


Output:

Before calling addAll()
List of Students Name:
 | Ram |  | Mohan |  | Sohan |  | Rabi | 

After calling addAll()
List of Students Name:
 | Ram |  | Mohan |  | Sohan |  | Rabi |  | Rohan |  | Kartik |

Program 3: Program to demonstrate Exception thrown by addAll() method. 

Java




// Java Program Demonstrate Exception thrown by addAll()
// method of ArrayDeque
 
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
 
        // create an ArrayDeque which going to
        // contains a list of Numbers
        ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
 
        // Add Number to list
        Numbers.add(223);
        Numbers.add(132);
        Numbers.add(345);
        Numbers.add(563);
 
        // create a collection of Number which is null
        ArrayList<Integer> col = null;
 
        try {
            // call addAll() method
            Numbers.addAll(col);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#addAll(java.util.Collection)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments