Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaCopyOnWriteArrayList addAll() method in Java with Examples

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 Value: This method returns a boolean value. It returns true if the collection is inserted correctly into the list, otherwise, it returns false.

Exception: This method throws NullPointerException if the specified element is null.




// Java Program to illustrate the
// CopyOnWriteArrayList.addAll(Collection c)
  
import java.io.*;
import java.util.concurrent.CopyOnWriteArrayList;
  
public class CopyOnWriteArrayListDemo {
    public static void main(String args[])
    {
  
        // create an empty array list1 with initial
        // capacity as 5
        CopyOnWriteArrayList<Integer> list1
            = new CopyOnWriteArrayList<Integer>();
  
        // use add() method to add elements in the list
        list1.add(12);
        list1.add(20);
        list1.add(45);
  
        // prints all the elements available in list1
        System.out.println("Printing list1:");
        for (Integer number : list1)
            System.out.println("Number = " + number);
  
        // create an empty array list2 with an initial
        // capacity
        CopyOnWriteArrayList<Integer> list2
            = new CopyOnWriteArrayList<Integer>();
  
        // use add() method to add elements in list2
        list2.add(25);
        list2.add(30);
        list2.add(31);
        list2.add(35);
  
        // let us print all the elements available in
        // list2
        System.out.println("Printing list2:");
        for (Integer number : list2)
            System.out.println("Number = " + number);
  
        // inserting all elements, list2 will get printed
        // after list1
        list1.addAll(list2);
  
        System.out.println("Printing all the elements");
        // let us print all the elements available in
        // list1
        for (Integer number : list1)
            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 is to add the elements of one collection into another list. Insertion of elements will start from the stated index. Elements will be added as per the order returned by the collection’s iterator. Element which was previously present at that index(if any) will move to right when a new element is added.

Parameters :
index is the position at which elements of collection will be inserted.
c is the collection containing the elements, which are to be appended at the end of list.

Return Value :Its return type is boolean. It returns true if the collection is inserted correctly into the list at the stated index, otherwise, it returns false.

Exception :
NullPointerException – if the specified collection is null.
IndexOutOfBoundsException – if the specified index is out of range.




// Java Program to illustrate the CopyOnWriteArrayList
// addAll(Collection c)
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class CopyOnWriteArrayListDemo {
    public static void main(String args[])
    {
  
        // create an empty array list1 with initial
        // capacity 5
        CopyOnWriteArrayList<Integer> list
            = new CopyOnWriteArrayList<Integer>();
  
        // using add() method to add elements in the
        // list
        list.add(12);
        list.add(20);
        list.add(45);
  
        // prints all the elements available in list1
        System.out.println("Printing list1:");
        for (Integer number : list)
            System.out.println("Number = " + number);
  
        // create an empty array list2 with an initial
        // capacity
        CopyOnWriteArrayList<Integer> list2
            = new CopyOnWriteArrayList<Integer>();
  
        // use add() method to add elements in list2
        list2.add(25);
        list2.add(30);
        list2.add(31);
        list2.add(35);
  
        // prints all the elements available in list2
        System.out.println("Printing list2:");
        for (Integer number : list2)
            System.out.println("Number = " + number);
  
        // inserting all elements of list2 at third
        // position
        list.addAll(2, list2);
  
        System.out.println("Printing all the elements");
  
        // prints all the elements available in list1
        for (Integer number : list)
            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
RELATED ARTICLES

Most Popular

Recent Comments