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

Java LinkedList removeFirst() Method

Java LinkedList removeFirst() Method

Java LinkedList Java LinkedList


removeFirst() is a method in Java's LinkedList is a method provided by the class for removing and returning the first element of the list. This method belongs to java.util.LinkedList class and is one of the basic operations of the doubly linked list data structure.

Method Syntax


public E removeFirst()

Parameters

This methodNot requireddoes not accept any parameters.

Return Value


Method behavior

Normal case

When the list is not empty:

  1. The method removes the first element from the list
  2. Returns the removed element
  3. The original second element becomes the new first element
  4. The list's size decreases by 1

Exception cases

When the list is empty (size = 0), calling this method throws NoSuchElementException exception.


Usage examples

Basic example

Example

import

java.util.LinkedList

;

public

class

LinkedListDemo

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a LinkedList

        LinkedList

<
String
fruits
=
new
LinkedList
<>
(
)

;

       

       

// Add elements

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Cherry"

)

;

       

       

System

.

out

.

println

(

"Original linked list: "

fruits

)

;

 

// Output: [Apple, Banana, Cherry]

       

       

// Use the removeFirst() method

       

String

firstFruit

=

fruits.

removeFirst

(

)

;

       

       

System

.

out

.

println

(

"Removed element: "

firstFruit

)

;

 

// Output: Apple

       

System

.

out

.

println

(

"List after operation: "

fruits

)

;

   

// Output: [Banana, Cherry]

   

}

}

Handle the empty list case

Example

import

java.util.LinkedList

;

import

java.util.NoSuchElementException

;

public

class

EmptyLinkedListDemo

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<
Integer
numbers
=
new
LinkedList
<>
(
)

;

       

       

try

{

           

int

first

=

numbers.

removeFirst

(

)

;

           

System

.

out

.

println

(

"Removed element: "

first

)

;

       

}

catch

(

NoSuchElementException

e

)

{

           

System

.

out

.

println

(

"List is empty, cannot remove element!"

)

;

       

}

   

}

}


Comparison with related methods

Method Names Description Behavior when the list is empty
removeFirst() Remove and return the first element Throws NoSuchElementException
pollFirst() Remove and return the first element Returns null
pop() Remove and return the first element (stack operation) Throws NoSuchElementException
remove() Remove and return the first element Throws NoSuchElementException

Practical Application Scenarios

  1. Queue processing: When using LinkedList as a queue, removeFirst() can be used to implement FIFO (first-in-first-out) operations
  2. Undo operation: In some applications, you may need to remove the most recently added operation record
  3. Task scheduling: Process the first task in a task list

Performance considerations

removeFirst() method has a time complexity of O(1) because:


Best Practices

  1. Empty list check: Before using removeFirst(), it is best to check whether the list is empty
  2. Alternative methods: If you do not want to handle exceptions, you can consider using the pollFirst() method
  3. Pairs with addFirst(): Often used together with the addFirst() method to implement a stack structure

Example

LinkedList

<
String
stack
=
new
LinkedList
<>
(
)

;

stack.

addFirst

(

"First"

)

;

 

// Push onto stack

stack.

addFirst

(

"Second"

)

;

// Push onto stack

String

top

=

stack.

removeFirst

(

)

;

// Pop from stack


Summary

LinkedList.removeFirst() is an efficient method for removing and returning the first element of the list. Understanding this method's behavior and exceptions is very important for using LinkedList correctly. In actual development, depending on whether you need to handle the empty list case, you can choose to use removeFirst() or pollFirst().

Java LinkedList Java LinkedList