Skip to main content

3. boolean addAll(Collection c)

3. boolean addAll(Collection c)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
 
public class AddAllMethod 
{
 
	public static void main(String[] args) 
	{
 
		LinkedList<String> list1=new LinkedList<String>();
 
        list1.add("Bhavesh");
        list1.add("Abhilash");
 
 
        //retrieving list1 element
 
        System.out.println("Retrieving list 1 element");
        for (String s : list1) 
        {
 
        	System.out.println(s);
 
		}
 
 
        LinkedList<String> list2=new LinkedList<String>();
 
        list2.add("Vivek");
        list2.add("milan");
 
        //Adding All element of list 1 into list 2
 
        list2.addAll(list1);
 
 
        System.out.println("Retrieving list 2:");
 
        for (String s : list2) 
        {
 
        	System.out.println(s);
		}
	}
}

output

1
2
3
4
5
6
7
8
Retrieving list 1 element
Bhavesh
Abhilash
Retrieving list 2:
Vivek
milan
Bhavesh
Abhilash