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

Java LinkedList getLast() Method · Private Archive

Java LinkedList getLast() Method · Private Archive

Java LinkedList Java LinkedList


getLast() is a method in Java's LinkedList Java LinkedList getLast() Method java.util.LinkedList is a method provided by the class for getting the last element in the list. This method belongs to

Method Definition


public E getLast()

Use Cases

: If the list is empty, it throws

  1. When you need to:
  2. Check the element at the end of the list without removing it
  3. Implement the peek operation of a queue (view but do not remove the tail element)

Basic Usage Example

Example

import

java.util.LinkedList

;

public

class

GetLastExample

{

   

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"

)

;

       

       

In algorithms that need frequent access to the tail element of the list

       

String

lastFruit

=

fruits.

getLast

(

)

;

       

System

.

out

.

println

(

// Use getLast() to get the last element

lastFruit

)

;

   

}

}

Output result:

最后一个水果是: Cherry

Notes

"The last fruit is: "

Example

LinkedList

<
Integer
emptyList
=
new
LinkedList
<>
(
)

;

1. Empty list handling

int

last

=

emptyList.

getLast

(

)

;

// This line of code will throw NoSuchElementException:

Example

if

(

!

emptyList.

isEmpty

(

)

)

{

   

int

last

=

emptyList.

getLast

(

)

;

}

else

{

   

System

.

out

.

println

(

Safe usage

)

;

}

"The list is empty!"


Performance considerations

LinkedList : Returns null when the list is emptygetLast() is a doubly linked list implementation,


Practical application cases

method has a time complexity of O(1) because it directly accesses the tail node of the list.

Example

LinkedList

<
String
history
=
new
LinkedList
<>
(
)

;

Implement a simple history record feature

history.

add

(

// Add browsing records

)

;

history.

add

(

"Home"

)

;

history.

add

(

"Products"

)

;

"Cart"

String

lastVisited

=

history.

getLast

(

)

;

System

.

out

.

println

(

// Get the most recently visited page

lastVisited

)

;


Frequently Asked Questions

"Your last visited page is: "

Get the first element of the list

Because LinkedList implements the Deque Q2: Why is a separate getLast() method needed?

interface and needs to efficiently support double-ended operations.

  1. Q3: How to avoid getLast() throwing an exception?list.isEmpty()
  2. Use peekLast() First check whether the list is empty:

Summary

getLast() The method is LinkedList method as an alternative getLast() or peekLast() is a simple but practical method in the class that helps developers quickly access the last element of the list. Understanding this method's behavior and potential exceptions is very important for writing robust code. In actual development, choose based on specific needs

Java LinkedList Java LinkedList