In the following example, we will show you the method of adding allÃ? elements of a specified collection (list) at the end of the list using addAll(Collection c) method of List Interface in java.Ã?Â
addAll(Collection c) method of List Interface in java.
In the following example, we will show you the method of adding all elements of a specified collection (list) at the end of the list using addAll(Collection c) method of List Interface in java.
Syntax
boolean addAll(Collection c)
This method returns either True or False value.
In this method you can insert a specified collection into a list. It inserts all elements of the specified collection into a list at the end of specified list. Elements of a collection is to be added to the specified collection in their chronological order, by this method. The behavior of this operation is uninfluenced if the specified collection is modified while the operation is in progress.
parameter description:-
Collection c : It takes an argument as collection (List) which do you want to add to the list.
Example of addAll(Collection c) method
In this example we will show you how does an addAll (Collection c) method work in List interface. This Example will help you to understand how a collection can be added to the list. Through this example we will show you how all elements of a list can be added to a list, and count the number of elements from list before and after adding the elements.
Example:-
package Devmanuals.com; import java.util.List; import java.util.ArrayList; public class ListAddAll { public static void main(String[] a) { List olist = new ArrayList(); olist.add("Dev"); olist.add("Manuals"); System.out.println("The elements of list1 are: " + olist); System.out.println("The size of list1 : " + olist.size()); List ls = new ArrayList(); ls.add(1); ls.add(2); System.out.println("The elements of list 2 are: " + ls); System.out.println("The size of list2 : " + ls.size()); boolean bool = ls.addAll(olist); System.out.println("The list after addition" + ls); System.out.println("The value are added TRUE/FALSE :" + bool); System.out.println("The size of list after addition : " + ls.size()); } }
Output:-
The elements of list1 are: [Dev, Manuals] The size of list1 : 2 The elements of list 2 are: [1, 2] The size of list2 : 2 The list after addition[1, 2, Dev, Manuals] The value are added TRUE/FALSE :true The size of list after addition : 4 |
[ 0 ] Comments