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 addLast(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.
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.
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.
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.
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.