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

Java LinkedList add() Method

Java LinkedList add() Method

Java LinkedList Java LinkedList


The LinkedList class provides several overloaded versions of the add() method for adding elements to the list.

1. boolean add(E e)

This is the most basic add method; it appends the specified element to the end of the list.

Example

LinkedList

<
String
list
=
new
LinkedList
<>
(
)

;

list.

add

(

"Apple"

)

;

list.

add

(

"Banana"

)

;

list.

add

(

"Cherry"

)

;

Method Characteristics:

2. void add(int index, E element)

Inserts the specified element at the specified position in the list.

Example

LinkedList

<
String
list
=
new
LinkedList
<>
(
)

;

list.

add

(

"Apple"

)

;

list.

add

(

"Banana"

)

;

list.

add

(

1

,

"Orange"

)

;

// Insert at index 1

Method Characteristics:

3. boolean addAll(Collection c)

Appends all elements of the specified collection to the end of the list, in the order returned by the iterator.

Example

LinkedList

<
String
fruits
=
new
LinkedList
<>
(
)

;

fruits.

add

(

"Apple"

)

;

fruits.

add

(

"Banana"

)

;

List

<
String
moreFruits
=
Arrays

.

asList

(

"Orange"

,

"Grape"

)

;

fruits.

addAll

(

moreFruits

)

;

Method Characteristics:

4. boolean addAll(int index, Collection c)

Inserts all elements of the specified collection into this list, starting at the specified position.

Example

LinkedList

<
String
fruits
=
new
LinkedList
<>
(
)

;

fruits.

add

(

"Apple"

)

;

fruits.

add

(

"Banana"

)

;

List

<
String
moreFruits
=
Arrays

.

asList

(

"Orange"

,

"Grape"

)

;

fruits.

addAll

(

1

, moreFruits

)

;

// Insert collection at index 1

Method Characteristics:


Comparison with Other Methods

add() vs addLast()

add() vs offer()

add() vs push()


Performance considerations

Efficiency of Add Operations

Comparison with ArrayList

Operation LinkedList ArrayList
Add at End O(1) O(1) amortized
Add at Beginning O(1) O(n)
Add in the Middle O(n) O(n)
random access O(n) O(1)

Practical Application Examples

Create a To-Do List

Example

LinkedList

<
String
todoList
=
new
LinkedList
<>
(
)

;

// Add tasks

todoList.

add

(

"Buy groceries"

)

;

todoList.

addFirst

(

"Check emails"

)

;

// High-priority task

todoList.

add

(

1

,

"Call mom"

)

;

// Medium priority

System

.

out

.

println

(

todoList

)

;

// Output: [Check emails, Call mom, Buy groceries]

Implement a Simple Queue

Example

LinkedList

<
String
queue
=
new
LinkedList
<>
(
)

;

// Enqueue operation

queue.

add

(

"First"

)

;

queue.

add

(

"Second"

)

;

queue.

add

(

"Third"

)

;

// Dequeue operation

while

(

!

queue.

isEmpty

(

)

)

{

   

System

.

out

.

println

(

queue.

removeFirst

(

)

)

;

}

Merge Two Lists

Example

LinkedList

<
String
list1
=
new
LinkedList
<>
(
Arrays

.

asList

(

"A"

,

"B"

,

"C"

)

)

;

LinkedList

<
String
list2
=
new
LinkedList
<>
(
Arrays

.

asList

(

"D"

,

"E"

,

"F"

)

)

;

list1.

addAll

(

list2

)

;

System

.

out

.

println

(

list1

)

;

// Output: [A, B, C, D, E, F]


Notes

  1. Thread Safety: LinkedList is not thread-safe; external synchronization is required in a multi-threaded environment
  2. null Element: Allows adding null elements
  3. Index Checking: Pay attention to boundary conditions when using the indexed add method
  4. Concurrent Modification: Modifying the list while using an iterator will throw ConcurrentModificationException

Java LinkedList Java LinkedList