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

Java LinkedList remove() Method

Java LinkedList remove() Method

Java LinkedList Java LinkedList


remove() method is the Java LinkedList is an important method in the class for removing elements from the list. LinkedList is part of the Java Collections Framework and implements the List and Deque interfaces.

remove() The method has several different forms, each with a specific purpose:

E remove

(

)

         

// Remove and return the first element of the list

E remove

(

int

index

)

 

// Remove the element at the specified position

boolean

remove

(

Object

o

)

// Remove the first occurrence of the specified element


Method details

remove() - no-argument form

Example

public

E remove

(

)

Function: Removes and returns the first element of this list.

Parameters: None

Return Value: The removed element

exception: If the list is empty, throws NoSuchElementException

Example:

Example

LinkedList

<
String
list
=
new
LinkedList
<>
(
)

;

list.

add

(

"Apple"

)

;

list.

add

(

"Banana"

)

;

list.

add

(

"Cherry"

)

;

String

first

=

list.

remove

(

)

;

// Remove and return "Apple"

System

.

out

.

println

(

"Removed: "

first

)

;

// Output: Removed: Apple

System

.

out

.

println

(

list

)

;

// Output: [Banana, Cherry]

remove(int index) - with index parameter

Example

public

E remove

(

int

index

)

Function: Removes the element at the specified position in the list.

Parameters:

Return Value: The removed element

exception:

Example:

Example

LinkedList

<
Integer
numbers
=
new
LinkedList
<>
(
)

;

numbers.

add

(

10

)

;

numbers.

add

(

20

)

;

numbers.

add

(

30

)

;

int

removed

=

numbers.

remove

(

1

)

;

// Remove the element at index 1 (20)

System

.

out

.

println

(

"Removed: "

removed

)

;

// Output: Removed: 20

System

.

out

.

println

(

numbers

)

;

// Output: [10, 30]

remove(Object o) - with object parameter

Example

public

boolean

remove

(

Object

o

)

Function: Removes the first occurrence of the specified element in the list (if it exists).

Parameters:

Return Value:

Example:

Example

LinkedList

<
String
fruits
=
new
LinkedList
<>
(
)

;

fruits.

add

(

"Apple"

)

;

fruits.

add

(

"Banana"

)

;

fruits.

add

(

"Apple"

)

;

boolean

result

=

fruits.

remove

(

"Apple"

)

;

// Remove the first "Apple"

System

.

out

.

println

(

"Removed? "

result

)

;

// Output: Removed? true

System

.

out

.

println

(

fruits

)

;

// Output: [Banana, Apple]


method to compare

Method form Parameter type Return Value exception Purpose
remove() None E (element type) NoSuchElementException Remove and return the first element
remove(int index) int E (element type) IndexOutOfBoundsException Remove the element at the specified position
remove(Object o) Object boolean None Remove the first occurrence of the specified element

Usage notes

Performance considerations

LinkedList's remove() method's performance in different situations:

  1. Removing a head element (remove() or remove(0)): O(1) time complexity
  2. Removing a tail element (remove(size()-1)): O(1) time complexity
  3. Removing a middle element: O(n) time complexity, because it needs to traverse to the specified position

Concurrent Modification

When traversing LinkedList using an iterator and calling remove() method may lead to ConcurrentModificationException. The correct approach is to use the iterator's own remove() method.

Incorrect Example:

Example

LinkedList

<
Integer
list
=
new
LinkedList
<>
(
Arrays

.

asList

(

1

,

2

,

3

)

)

;

for

(

Integer

num

:

list

)

{

   

if

(

num

==

2

)

{

        list.

remove

(

num

)

;

// May throw ConcurrentModificationException

   

}

}

Correct approach:

Example

Iterator

<
Integer
it
=

list.

iterator

(

)

;

while

(

it.

hasNext

(

)

)

{

   

Integer

num

=

it.

next

(

)

;

   

if

(

num

==

2

)

{

        it.

remove

(

)

;

// Use the iterator's remove() method

   

}

}

null Value Handling

LinkedList allows storing null values; you can use remove(null) to remove null elements from the list.

Example

LinkedList

<
String
list
=
new
LinkedList
<>
(
)

;

list.

add

(

"A"

)

;

list.

add

(

null

)

;

list.

add

(

"B"

)

;

list.

remove

(

null

)

;

// Remove null element

System

.

out

.

println

(

list

)

;

// Output: [A, B]


Practical Application Examples

Implementing a queue

LinkedList's remove() method can be used to implement the FIFO (first-in-first-out) characteristic of a queue:

Example

LinkedList

<
String
queue
=
new
LinkedList
<>
(
)

;

queue.

add

(

"Task1"

)

;

queue.

add

(

"Task2"

)

;

queue.

add

(

"Task3"

)

;

while

(

!

queue.

isEmpty

(

)

)

{

   

String

task

=

queue.

remove

(

)

;

// Remove and return the first element

   

System

.

out

.

println

(

"Processing: "

task

)

;

}

Remove elements that meet specific conditions

Example

LinkedList

<
Integer
numbers
=
new
LinkedList
<>
(
Arrays

.

asList

(

1

,

2

,

3

,

4

,

5

,

6

)

)

;

// Remove all even numbers

numbers.

removeIf

(

n

->

n

%

2

==

0

)

;

System

.

out

.

println

(

numbers

)

;

// Output: [1, 3, 5]

Comparison with ArrayList's remove()

Example

// LinkedList's remove() performs better in the middle

LinkedList

<
Integer
linkedList
=
new
LinkedList
<>
(
Arrays

.

asList

(

1

,

2

,

3

,

4

,

5

)

)

;

linkedList.

remove

(

2

)

;

// Relatively efficient

// ArrayList's remove() needs to move elements in the middle

ArrayList

<
Integer
arrayList
=
new
ArrayList
<>
(
Arrays

.

asList

(

1

,

2

,

3

,

4

,

5

)

)

;

arrayList.

remove

(

2

)

;

// Needs to move subsequent elements


Summary

LinkedList's remove() method provides multiple forms to meet different removal needs:

  1. remove() - Quickly remove the head element, suitable for queue operations
  2. remove(int index) - Precisely control the position of the element to remove
  3. remove(Object o) - Remove based on the element value

In practical applications, you should choose the appropriate method form based on specific needs and pay attention to their performance characteristics and exceptions. Understanding the differences and applicable scenarios of these methods will help you use LinkedList, an important collection class, more effectively.

Java LinkedList Java LinkedList