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.
import java.io.*;
import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteArrayListDemo {
public static void main(String args[])
{
CopyOnWriteArrayList<Integer> list1
= new CopyOnWriteArrayList<Integer>();
list1.add( 12 );
list1.add( 20 );
list1.add( 45 );
System.out.println( "Printing list1:" );
for (Integer number : list1)
System.out.println( "Number = " + number);
CopyOnWriteArrayList<Integer> list2
= new CopyOnWriteArrayList<Integer>();
list2.add( 25 );
list2.add( 30 );
list2.add( 31 );
list2.add( 35 );
System.out.println( "Printing list2:" );
for (Integer number : list2)
System.out.println( "Number = " + number);
list1.addAll(list2);
System.out.println( "Printing all the elements" );
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.
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
public class CopyOnWriteArrayListDemo {
public static void main(String args[])
{
CopyOnWriteArrayList<Integer> list
= new CopyOnWriteArrayList<Integer>();
list.add( 12 );
list.add( 20 );
list.add( 45 );
System.out.println( "Printing list1:" );
for (Integer number : list)
System.out.println( "Number = " + number);
CopyOnWriteArrayList<Integer> list2
= new CopyOnWriteArrayList<Integer>();
list2.add( 25 );
list2.add( 30 );
list2.add( 31 );
list2.add( 35 );
System.out.println( "Printing list2:" );
for (Integer number : list2)
System.out.println( "Number = " + number);
list.addAll( 2 , list2);
System.out.println( "Printing all the elements" );
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
Last Updated :
30 Jan, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...