The Wayback Machine - https://web.archive.org/web/20240918154912/https://www.geeksforgeeks.org/java-util-arraylist-addall-method-java/
Open In App

Java.util.ArrayList.addall() method in Java

Last Updated : 26 Nov, 2018
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Below are the addAll() methods of ArrayList in Java:

  1. boolean addAll(Collection c) : This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress(implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty).
    Parameters:
    c : This is the collection containing elements to be added to this list.
    Exception:
    NullPointerException : If the specified collection is null




    // Java program to illustrate
    // boolean addAll(Collection c)
    import java.io.*;
    import java.util.ArrayList;
      
    public class ArrayListDemo {
        public static void main(String args[])
        {
      
            // create an empty array list1 with initial 
            // capacity as 5
            ArrayList<Integer> arrlist1 = 
                               new ArrayList<Integer>(5);
      
            // use add() method to add elements in the list
            arrlist1.add(12);
            arrlist1.add(20);
            arrlist1.add(45);
      
            // prints all the elements available in list1
            System.out.println("Printing list1:");
            for (Integer number : arrlist1) 
                System.out.println("Number = " + number);        
      
            // create an empty array list2 with an initial
            // capacity
            ArrayList<Integer> arrlist2 = 
                                 new ArrayList<Integer>(5);
      
            // use add() method to add elements in list2
            arrlist2.add(25);
            arrlist2.add(30);
            arrlist2.add(31);
            arrlist2.add(35);
      
            // let us print all the elements available in 
            // list2
            System.out.println("Printing list2:");
            for (Integer number : arrlist2) 
                System.out.println("Number = " + number);        
      
            // inserting all elements, list2 will get printed
            // after list1
            arrlist1.addAll(arrlist2);
      
            System.out.println("Printing all the elements");
            // let us print all the elements available in 
            // list1
            for (Integer number : arrlist1) 
                System.out.println("Number = " + number);        
        }
    }

    
    

    Output:Printing list1:
    Number = 12
    Number = 20
    Number = 45
    Printing list2:
    Number = 25
    Number = 30
    Number = 31
    Number = 35
    Printing all the elements
    Number = 12
    Number = 20
    Number = 45
    Number = 25
    Number = 30
    Number = 31
    Number = 35
    

    boolean addAll(int index, Collection c):This method inserts all of the elements in the specified collection into this list, starting at the specified position. It shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection’s iterator.

    Parameters:
    index : The index at which to insert the first element from the specified collection.
    c : This is the collection containing elements to be added to this list.
    Exception:
    IndexOutOfBoundsException : If the index is out of range
    NullPointerException : If the specified collection is null




    // Java program to illustrate
    // boolean addAll(int index, Collection c)
    import java.io.*;
    import java.util.ArrayList;
      
    public class ArrayListDemo {
        public static void main(String args[])
        {
      
            // create an empty array list1 with initial
            // capacity 5
            ArrayList<Integer> arrlist = 
                            new ArrayList<Integer>(5);
      
            // using add() method to add elements in the 
            // list
            arrlist.add(12);
            arrlist.add(20);
            arrlist.add(45);
      
            // prints all the elements available in list1
            System.out.println("Printing list1:");
            for (Integer number : arrlist) 
                System.out.println("Number = " + number);        
      
            // create an empty array list2 with an initial
            // capacity
            ArrayList<Integer> arrlist2 = 
                                new ArrayList<Integer>(5);
      
            // use add() method to add elements in list2
            arrlist2.add(25);
            arrlist2.add(30);
            arrlist2.add(31);
            arrlist2.add(35);
      
            // prints all the elements available in list2
            System.out.println("Printing list2:");
            for (Integer number : arrlist2) 
                System.out.println("Number = " + number);        
      
            // inserting all elements of list2 at third 
            // position
            arrlist.addAll(2, arrlist2);
      
            System.out.println("Printing all the elements");
      
            // prints all the elements available in list1
            for (Integer number : arrlist) 
                System.out.println("Number = " + number);        
        }
    }

    
    

    Output:Printing list1:
    Number = 12
    Number = 20
    Number = 45
    Printing list2:
    Number = 25
    Number = 30
    Number = 31
    Number = 35
    Printing all the elements
    Number = 12
    Number = 20
    Number = 25
    Number = 30
    Number = 31
    Number = 35
    Number = 45
    


Previous Article
Next Article

Similar Reads

Java.util.ArrayList.add() Method in Java
Below are the add() methods of ArrayList in Java: boolean add(Object o) : This method appends the specified element to the end of this list. Parameters: object o: The element to be appended to this list. Exception: NA // Java code to illustrate add(Object o) import java.io.*; import java.util.ArrayList; public class ArrayListDemo { public static vo
2 min read
Java.util.Arraylist.indexOf() in Java
The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Syntax : public int IndexOf(Object o) obj : The element to search for. // Java code to demonstrate the working of // indexOf in ArrayList // for ArrayList functions import java.util.Arra
2 min read
LinkedList addAll() Method in Java
java.util.LinkedList.addAll(Collection C): This method is used to append all of the elements from the collection passed as parameter to this function to the end of a list keeping in mind the order of return by the collections iterator. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of ArrayList. It is the collectio
3 min read
TreeSet addAll() Method in Java
The java.util.TreeSet.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the tree set. Return Valu
2 min read
Vector addAll() Method in Java
java.util.Vector.addAll(Collection C): This method is used to append all of the elements from the collection passed as a parameter to this function to the end of a vector keeping in mind the order of return by the collection's iterator. Syntax: boolean addAll(Collection C) Parameters: The method accepts a mandatory parameter C which is a collection
2 min read
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 les
4 min read
ConcurrentLinkedQueue addAll() method in Java
The addAll() method of ConcurrentLinkedQueue is used to inserts all the elements of the Collection, passed as parameter to this method, at the end of a ConcurrentLinkedQueue. The insertion of element is in same order as returned by the collections iterator. Syntax: public boolean addAll(Collection&lt;? extends E&gt; c) Parameter: This method takes
4 min read
Collections addAll() method in Java with Examples
The addAll() method of java.util.Collections class is used to add all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under
3 min read
AbstractList addAll() method in Java with Examples
The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that th
4 min read
AbstractQueue addAll() method in Java with examples
The addAll(E e) method of AbstractQueue adds all of the elements in the specified collection to this queue. Syntax: public boolean addAll(Collection c) Parameters: This method accepts a mandatory parameter collection containing elements to be added to this queue Returns: The method returns true if this queue changed as a result of the call Exceptio
3 min read
AbstractCollection addAll() Method in Java with Examples
The addAll(Collection C) method of Java AbstractCollection is used to append all of the elements from a mentioned collection to this collection. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the collection. Ret
2 min read
AbstractSequentialList addAll() Method in Java with Examples
The addAll(int index, Collection C) method of AbstractSequentialList is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a abstract sequential list. Syntax: boolean addAll(int index, Collection C) Parameters: This function accepts two parameters as shown in the above sy
2 min read
Collection addAll() method in Java with Examples
The addAll(Collection collection) of java.util.Collection interface is used to add the Collection 'collection' to this existing collection. This method returns a boolean value depicting the successfulness of the operation. If the collection was added, it returns true, else it returns false. Syntax: Collection.addAll(Collection&lt;E&gt; collection)
3 min read
ConcurrentLinkedDeque addAll() method in Java with Examples
The addAll(Collection col) of ConcurrentLinkedDeque which takes col as a parameter, where col is a Collection of elements (List, ArrayList, LinkedList etc). This entire Collection gets appended or added to the end of the Dequeue. This method just like add() method returns true if the Collection gets appended to the Deque. Syntax: public boolean add
3 min read
Stack addAll(int, Collection) method in Java with Example
The addAll(int, Collection) method of Stack Class is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a Stack. Syntax: boolean addAll(int index, Collection C) Parameters: This function accepts two parameters as shown in the above syntax and are described below. index: T
2 min read
Stack addAll(Collection) method in Java with Example
The addAll(Collection) method of Stack Class is used to append all of the elements from the collection passed as a parameter to this function to the end of a Stack keeping in mind the order of return by the collection's iterator. Syntax: boolean addAll(Collection C) Parameters: The method accepts a mandatory parameter C which is a collection of Arr
2 min read
Set addAll() method in Java with Examples
The java.util.Set.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the set. Return Value: The me
2 min read
List addAll() Method in Java with Examples
This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. Syntax: boolean addAll(Collection c) Parameters: This function has a single parameter, i.e, Collection c, whose elements are to be appended to the list. Returns: It returns true if
2 min read
CopyOnWriteArraySet addAll() method in Java with Examples
The addAll() method of CopyonWriteArraySet method adds all the element of the specified collection to this CopyOnWriteArraySet which are not present in it. This methods results the union of the two collections. Syntax: public boolean addAll(Collection&lt;E&gt; c) Parameters: This method accepts a parameter c which is the collection containing eleme
2 min read
CopyOnWriteArrayList addAll() method in Java with Examples
boolean addAll(Collection c) This method is used to append all the elements identified by the collection c at the end of the list, in the same order returned by the collection's iterator. Parameters: This method accepts a mandatory parameter c which is the collection containing the elements, which are to be appended at the end of the list. Return V
4 min read
LinkedBlockingDeque addAll() method in Java with Examples
The addAll() method of LinkedBlockingDeque appends all of the elements of the specified collection to the end of this deque.Syntax: public void addAll(Collection&lt;E&gt; c) Parameters: This method accepts a mandatory parameter c which is the collection to be inserted in the end of the LinkedBlockingDeque.Return Value: This method does not returns
2 min read
SortedSet addAll() method in Java with Examples
The addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the set. Return Value: The method returns t
2 min read
NavigableSet addAll() method in Java
The java.util.NavigableSet.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing NavigableSet. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the Navigab
2 min read
CompositeName addAll() method in Java with Examples
The addAll() method of a javax.naming.CompositeName class is used to add all the component of a different composite name object to this CompositeName object.There are two different addAll() methods. 1. addAll(int, Name): This method is used to add all the component of a different composite name object which is passed as a parameter at a specified p
3 min read
CompoundName addAll() method in Java with Examples
The addAll() method of a javax.naming.CompoundName class is used to add all the components of a different compound name object to this CompoundName object. There are two different addAll() methods. 1. addAll(int, Name): This method is used to add All the components of a different Compound name object which is passed as a parameter at a specified po
3 min read
Java.util.Collections.rotate() Method in Java with Examples
java.util.Collections.rotate() method is present in java.util.Collections class. It is used to rotate the elements present in the specified list of Collection by a given distance. Syntax: public static void rotate(List&lt; type &gt; list, int distance) Parameters : list - the list to be rotated. distance - the distance to rotate the list. type - Ty
2 min read
Java.util.Collections.disjoint() Method in java with Examples
java.util.Collections.disjoint() method is present in java.util.Collections class. It is used to check whether two specified collections are disjoint or not. More formally, two collections are disjoint if they have no elements in common. Syntax: public static boolean disjoint(Collection&lt;?&gt; c1, Collection&lt;?&gt; c2) Parameters : c1 - a colle
3 min read
Java.util.BitSet.set() method in Java
There are four variants of set() method.This article depicts about all of them, as follows: 1. set(int Index) : This method sets the bit at the specified index to true i.e adds a value. Declaration : public void set(int bitIndex) Parameters : Index : a bit index. Result : This method does not return a value. Exception : IndexOutOfBoundsException :
3 min read
java.util.Calendar.after() method
java.util.Calendar.after() is a method in Calendar class of java.util package. The method returns true if the time represented by this Calendar is after the time represented by when Object. If it is not the case, false is returned. Syntax : public boolean after(Object when) Where, when is the Object that is to be compared. Below are some examples t
2 min read
ArrayList get(index) Method in Java with Examples
The get() method of ArrayList in Java is used to get the element of a specified index within the list. Syntax: get(index) Parameter: Index of the elements to be returned. It is of data-type int. Return Type: The element at the specified index in the given list. Exception: It throws IndexOutOfBoundsException if the index is out of range (index=size(
2 min read