Java LinkedList addAll() Method
addAll()In Java, the method is aLinkedListcommon method provided by the class, used to add all elements from one collection to another.
Method Syntax
LinkedListThe class provides twoaddAll()overloaded forms of the method:
1. Add elements at the end of the list
boolean addAll(Collection<? extends E> c)
2. Insert elements at a specified position
boolean addAll(int index, Collection<? extends E> c)
Parameter Description
First form parameters
c: The collection elements to be added, where the element type must be compatible with the element type of LinkedList
Second form parameters
index: The index of the insertion position (starting from 0)c: The collection elements to be added
Return Value
Both methods return abooleanvalue:
trueIf the LinkedList changes as a result of the callfalseIf the collection passed in is empty or no elements were added
Usage Example
Basic Usage Example
Example
import java.util.LinkedList;
import java.util.ArrayList;
public class AddAllExample {
public static void main(String[] args) {
// Create two collections
LinkedList<String> linkedList = new LinkedList<>();
ArrayList<String> arrayList = new ArrayList<>();
// Add elements to the ArrayList
arrayList.add("Apple");
arrayList.add("Banana");
arrayList.add("Orange");
// Use the addAll() method to add the ArrayList elements to the LinkedList
linkedList.addAll(arrayList);
System.out.println("LinkedList contents: " + linkedList);
}
}
import java.util.ArrayList;
public class AddAllExample {
public static void main(String[] args) {
// Create two collections
LinkedList<String> linkedList = new LinkedList<>();
ArrayList<String> arrayList = new ArrayList<>();
// Add elements to the ArrayList
arrayList.add("Apple");
arrayList.add("Banana");
arrayList.add("Orange");
// Use the addAll() method to add the ArrayList elements to the LinkedList
linkedList.addAll(arrayList);
System.out.println("LinkedList contents: " + linkedList);
}
}
Output result:
LinkedList 内容: [Apple, Banana, Orange]
Insert elements at a specified position
Example
import java.util.LinkedList;
import java.util.Arrays;
public class AddAllAtIndexExample {
public static void main(String[] args) {
LinkedList<String> fruits = new LinkedList<>(Arrays.asList("Apple", "Orange"));
LinkedList<String> moreFruits = new LinkedList<>(Arrays.asList("Banana", "Grape"));
// Insert elements at index 1
fruits.addAll(1, moreFruits);
System.out.println("Modified LinkedList: " + fruits);
}
}
import java.util.Arrays;
public class AddAllAtIndexExample {
public static void main(String[] args) {
LinkedList<String> fruits = new LinkedList<>(Arrays.asList("Apple", "Orange"));
LinkedList<String> moreFruits = new LinkedList<>(Arrays.asList("Banana", "Grape"));
// Insert elements at index 1
fruits.addAll(1, moreFruits);
System.out.println("Modified LinkedList: " + fruits);
}
}
Output result:
修改后的 LinkedList: [Apple, Banana, Grape, Orange]
Notes
- Empty collection handling: If the collection passed in is null, it will throw an exception.
NullPointerException - Index out of bounds: If the specified index is out of range (index < 0 || index > size()), it will throw an exception.
IndexOutOfBoundsException - Performance considerations: Inserting a large number of elements in the middle of a LinkedList may be more efficient than ArrayList
- Element order: The newly added elements will maintain their order in the original collection
Practical Application Scenarios
addAll()The method is particularly useful in the following scenarios:
- Merging data from multiple collections
- Batch initializing LinkedList contents
- Inserting a group of elements at a specific position
- Implementing collection copy or clone functionality
Comparison with ArrayList's addAll()
| Feature | LinkedList addAll() | ArrayList addAll() |
|---|---|---|
| Performance of adding at the end | O(1) | O(1) amortized time |
| Performance of inserting in the middle | O(n) | O(n) |
| Memory usage | Higher (each element has extra pointers) | Lower |
Other extensions
Java LinkedList