Java LinkedList addAll()
method
The addAll()
method in Java's LinkedList
allows you to add all elements from another collection (like an array, a set or another list) into your linked list. You can either append these elements at the end of the list or insert them at a specific index.
Syntax
public boolean addAll(Collection<? extends E> c)
public boolean addAll(int index, Collection<? extends E> c)
Parameters
Parameter | Description |
---|---|
c |
The collection whose elements are to be added to this list. |
index |
The index at which the collection is to be inserted. If the specified index is greater than the size of the LinkedList, an IndexOutOfBoundsException is thrown. |
Return Value
Both versions of addAll()
return a boolean
value. It returns true
if the addition was successful, and false
otherwise.
Examples
Example 1: Adding all elements at the end
This example demonstrates how to append all elements from an existing list to a linked list using the first version of addAll()
. Think of it like merging two lists together.
import java.util.*;
public class AddAllExample1 {
public static void main(String[] args) {
LinkedList<String> list1 = new LinkedList<>(Arrays.asList("A", "B", "C"));
List<String> list2 = Arrays.asList("D", "E", "F");
boolean added = list1.addAll(list2);
System.out.println("Added all elements: " + added); // Output will be true.
System.out.println(list1); // Prints the combined list.
}
}
Added all elements: true
[A, B, C, D, E, F]
In this example, list2
(containing "D", "E", and "F") is appended to the end of list1
. The resulting list contains all elements from both original lists.
Example 2: Adding all elements at a specific index
This example shows how to insert all elements from one collection into another linked list at a particular position using the second version of addAll()
. This is like inserting a new block of items into an existing list.
import java.util.*;
public class AddAllExample2 {
public static void main(String[] args) {
LinkedList<Integer> list1 = new LinkedList<>(Arrays.asList(1, 2, 3));
List<Integer> list2 = Arrays.asList(4, 5, 6);
boolean added = list1.addAll(1, list2);
System.out.println("Added all elements: " + added); // Output will be true.
System.out.println(list1); // Prints the modified list.
}
}
Added all elements: true
[1, 4, 5, 6, 2, 3]
Here, the elements of list2
(containing 4, 5, and 6) are inserted into list1
starting at index 1. The original element at index 1 (which was '2') and all subsequent elements are shifted to the right.
Example 3: Handling Exceptions
This example demonstrates what happens when you try to insert a collection at an invalid index, which leads to an exception.
import java.util.*;
public class AddAllExample3 {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B", "C"));
List<String> toAdd = Arrays.asList("D", "E");
try {
list.addAll(10, toAdd); // Attempt to insert at an invalid index.
} catch (IndexOutOfBoundsException e) {
System.out.println("Caught IndexOutOfBoundsException: " + e.getMessage());
}
}
}
Caught IndexOutOfBoundsException: index out of range
Because the LinkedList
only has a size of 3, attempting to insert at index 10 will throw an IndexOutOfBoundsException
. It's important to make sure your index is within valid bounds (between 0 and size()
).