- 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 collection whose elements are needed to be appended at the end of the list.
Return Value: The method returns true if at least one action of append is performed.
Below program illustrate the Java.util.LinkedList.addAll() method:
// Java code to illustrate boolean addAll()
import
java.util.*;
import
java.util.LinkedList;
import
java.util.ArrayList;
Â
Âpublic
class
LinkedListDemo {
  Â
public
static
void
main(String args[]) {
Â
ÂÂ Â Â Â Â Â
// Creating an empty LinkedList
     Â
LinkedList<String> list =
new
LinkedList<String>();
Â
ÂÂ Â Â Â Â Â
// Use add() method to add elements in the list
     Â
list.add(
"Geeks"
);
     Â
list.add(
"for"
);
     Â
list.add(
"Geeks"
);
     Â
list.add(
"10"
);
     Â
list.add(
"20"
);
      Â
ÂÂ Â Â Â Â Â
// A collection is created
     Â
Collection<String> collect =
new
ArrayList<String>();
     Â
collect.add(
"A"
);
     Â
collect.add(
"Computer"
);
     Â
collect.add(
"Portal"
);
     Â
collect.add(
"for"
);
     Â
collect.add(
"Geeks"
);
Â
ÂÂ Â Â Â Â Â
// Displaying the list
     Â
System.out.println(
"The LinkedList is: "
+ list);
            Â
ÂÂ Â Â Â Â Â
// Appending the collection to the list
     Â
list.addAll(collect);
Â
ÂÂ Â Â Â Â
// Clearing the list using clear() and displaying
    Â
System.out.println(
"The new linked list is: "
+ list);
Â
ÂÂ Â Â
}
}
Output:The LinkedList is: [Geeks, for, Geeks, 10, 20] The new linked list is: [Geeks, for, Geeks, 10, 20, A, Computer, Portal, for, Geeks]
- java.util.LinkedList.addAll(int index, Collection C): This method is used to append all of the elements from the collection passed as parameter to this function at a specific index or position of a list.
Syntax:
boolean addAll(int index, Collection C)
Parameters: This function accepts two parameters as shown in the above syntax and are described below.
- index: This parameter is of integer datatype and specifies the position in the list starting from where the elements from the container will be inserted.
- C: It is a collection of ArrayList. It is the collection whose elements are needed to be appended.
Return Value: The method returns TRUE if at least one action of append is performed.
Below program illustrate the Java.util.LinkedList.addAll() method:
// Java code to illustrate boolean addAll()
import
java.util.*;
import
java.util.LinkedList;
import
java.util.ArrayList;
Â
Âpublic
class
LinkedListDemo {
  Â
public
static
void
main(String args[]) {
Â
ÂÂ Â Â Â Â Â
// Creating an empty LinkedList
     Â
LinkedList<String> list =
new
LinkedList<String>();
Â
ÂÂ Â Â Â Â Â
// Use add() method to add elements in the list
     Â
list.add(
"Geeks"
);
     Â
list.add(
"for"
);
     Â
list.add(
"Geeks"
);
     Â
list.add(
"10"
);
     Â
list.add(
"20"
);
      Â
ÂÂ Â Â Â Â Â
// Creating a Collection
     Â
Collection<String> collect =
new
ArrayList<String>();
     Â
collect.add(
"A"
);
     Â
collect.add(
"Computer"
);
     Â
collect.add(
"Portal"
);
     Â
collect.add(
"for"
);
     Â
collect.add(
"Geeks"
);
Â
ÂÂ Â Â Â Â Â
// Displaying the list
     Â
System.out.println(
"The LinkedList is: "
+ list);
            Â
ÂÂ Â Â Â Â Â
// Appending the collection to the list
     Â
list.addAll(
1
, collect);
Â
ÂÂ Â Â Â Â
// Clearing the list using clear() and displaying
    Â
System.out.println(
"The new linked list is: "
+ list);
Â
ÂÂ Â Â
}
}
Output:The LinkedList is: [Geeks, for, Geeks, 10, 20] The new linked list is: [Geeks, A, Computer, Portal, for, Geeks, for, Geeks, 10, 20]