Java LinkedList addAll() Method
addAll() The method is in Java's LinkedList is a commonly used method of the class, used to add all elements of one collection to another. This method is very useful when handling batch data operations.
Method Syntax
LinkedList The class provides two addAll() overloaded forms of the method:
1. Add elements at the end of the list
boolean addAll(Collection extends E> c)
2. Insert elements at the specified position
boolean addAll(int index, Collection extends E> c)
Parameter Description
First form parameters
c: The collection elements to add, whose element type must be compatible with the element type of the LinkedList
Second form parameters
index: The index of the insertion position (starting from 0)c: The collection elements to add
Return Value
Both methods return boolean value:
true: If the LinkedList changed as a result of the callfalse: If the passed collection is empty or no elements were added
Usage examples
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 ArrayList elements to the LinkedList
linkedList.
addAll
(
arrayList
)
;
System
.
out
.
println
(
"LinkedList content: "
linkedList
)
;
}
}
Output result:
LinkedList 内容: [Apple, Banana, Orange]
inserts an element 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
)
;
}
}
Output result:
修改后的 LinkedList: [Apple, Banana, Grape, Orange]
Notes
- Empty Collection Handling: If the passed collection is null, a will be thrown
NullPointerException - Index Out of Bounds: If the specified index is out of range (index < 0 || index > size()), a will be thrown
IndexOutOfBoundsException - Performance considerations: Inserting a large number of elements in the middle of a LinkedList may be more efficient than with ArrayList
- Element Order: The newly added elements retain their order from the original collection
Practical Application Scenarios
addAll() The method is especially useful in the following scenarios:
- Merge data from multiple collections
- Batch-initialize LinkedList content
- Insert a group of elements at a specific position
- Implement collection copy or clone functionality
Comparison with ArrayList's addAll()
| Feature | LinkedList addAll() | ArrayList addAll() |
|---|---|---|
| Performance of Adding at End | O(1) | O(1) amortized time |
| Performance of Insertion in the Middle | O(n) | O(n) |
| Memory Usage | Higher (each element has an extra pointer) | Lower |
Java LinkedList