Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java LinkedList addAll() Method

Java LinkedList addAll() Method

Java LinkedList Java LinkedList


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 c)

2. Insert elements at the specified position


boolean addAll(int index, Collection c)

Parameter Description

First form parameters

Second form parameters


Return Value

Both methods return boolean value:


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

  1. Empty Collection Handling: If the passed collection is null, a will be thrown NullPointerException
  2. Index Out of Bounds: If the specified index is out of range (index < 0 || index > size()), a will be thrown IndexOutOfBoundsException
  3. Performance considerations: Inserting a large number of elements in the middle of a LinkedList may be more efficient than with ArrayList
  4. 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:

  1. Merge data from multiple collections
  2. Batch-initialize LinkedList content
  3. Insert a group of elements at a specific position
  4. 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 Java LinkedList