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

Java LinkedList get() Method

Java LinkedList get() Method

Java LinkedList Java LinkedList


get() The method is in Java's LinkedList is a commonly used method provided by the class for getting the element at a specified position in the list.LinkedList is in the Java Collections Framework List is an implementation class of the interface and uses a doubly linked list data structure to store elements.

get() The basic syntax of the method is as follows:


E get(int index)

Where:


How the get() method works

LinkedList 's get() The method gets the element at the specified position by traversing the list. Since LinkedList is implemented based on a linked list, unlike ArrayList which cannot directly access elements randomly by index.

When get(index) , theLinkedList will:

  1. Check whether the index is valid (0 ≤ index < size)
  2. Start traversing from the head or tail of the list (choose the nearer end to start)
  3. Move node by node until the node at the specified position is found
  4. Return the element stored in that node

Usage examples

Basic Usage

Example

import

java.util.LinkedList

;

public

class

LinkedListGetExample

{

   

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"

)

;

        fruits.

add

(

"Durian"

)

;

       

       

// Use the get() method to get elements

       

String

firstFruit

=

fruits.

get

(

0

)

;

 

// Get the first element

       

String

thirdFruit

=

fruits.

get

(

2

)

;

 

// Get the third element

       

       

System

.

out

.

println

(

"First fruit: "

firstFruit

)

;

 

// Output: Apple

       

System

.

out

.

println

(

"Third fruit: "

thirdFruit

)

;

 

// Output: Cherry

   

}

}

Traversing LinkedList

Example

import

java.util.LinkedList

;

public

class

LinkedListTraversal

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<
Integer
numbers
=
new
LinkedList
<>
(
)

;

        numbers.

add

(

10

)

;

        numbers.

add

(

20

)

;

        numbers.

add

(

30

)

;

        numbers.

add

(

40

)

;

       

       

// Traverse LinkedList using the get() method

       

for

(

int

i

=

0

;

i

<

numbers.

size

(

)

;

i

++

)

{

           

System

.

out

.

println

(

"Round "

i

" element: "

numbers.

get

(

i

)

)

;

       

}

   

}

}


Performance considerations

LinkedList 's get() method has a time complexity of O(n) because it needs to traverse the list from the head or tail to find the element at the specified position. This contrasts with ArrayList O(1) time complexity.

Therefore, if you need frequent random access to elements,ArrayList may be a better choice. While LinkedList performs better during frequent insertion and deletion operations.


Exception handling

When using get() method, if the passed index is out of range (index < 0 or index ≥ size), it throws IndexOutOfBoundsException.

Safe usage example

Example

import

java.util.LinkedList

;

public

class

SafeGetExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<
String
colors
=
new
LinkedList
<>
(
)

;

        colors.

add

(

"Red"

)

;

        colors.

add

(

"Green"

)

;

        colors.

add

(

"Blue"

)

;

       

       

int

index

=

3

;

 

// Index out of range

       

       

try

{

           

String

color

=

colors.

get

(

index

)

;

           

System

.

out

.

println

(

color

)

;

       

}

catch

(

IndexOutOfBoundsException

e

)

{

           

System

.

out

.

println

(

"Error: index "

index

" is out of range. List size is "

colors.

size

(

)

)

;

       

}

   

}

}


Alternative methods

Besides get() method,LinkedList also provides other methods for getting elements:

  1. getFirst(): Get the first element
  2. getLast(): Get the last element
  3. peek(): Get but do not remove the first element (returns null when the list is empty)
  4. peekFirst(): Same as peek()
  5. peekLast(): Get but do not remove the last element (returns null when the list is empty)

Example

Example

import

java.util.LinkedList

;

public

class

AlternativeMethods

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<
String
books
=
new
LinkedList
<>
(
)

;

        books.

add

(

"Java Programming"

)

;

        books.

add

(

"Data Structures"

)

;

        books.

add

(

"Algorithms"

)

;

       

       

System

.

out

.

println

(

"First book: "

books.

getFirst

(

)

)

;

       

System

.

out

.

println

(

"Last book: "

books.

getLast

(

)

)

;

       

System

.

out

.

println

(

"peek(): "

books.

peek

(

)

)

;

       

System

.

out

.

println

(

"peekLast(): "

books.

peekLast

(

)

)

;

   

}

}


Best Practices

  1. Avoid using the get() method frequently: Because LinkedList 's get() method has lower performance; if you need frequent random access, consider using ArrayList.

  2. Use an iterator to traverse: When you need to traverse LinkedList , using an iterator is more efficient than using get() method:

Example

// More efficient traversal method

for

(

String

fruit

:

fruits

)

{

   

System

.

out

.

println

(

fruit

)

;

}

// Or use an iterator

Iterator

<
String
iterator
=

fruits.

iterator

(

)

;

while

(

iterator.

hasNext

(

)

)

{

   

System

.

out

.

println

(

iterator.

next

(

)

)

;

}

  1. Check the index range: When using get() method, it is best to check whether the index is valid first:

Example

if

(

index

=

0

&&

index

<

list.

size

(

)

)

{

   

return

list.

get

(

index

)

;

}

else

{

   

// Handle the invalid index case

}


Summary

LinkedList 's get() method is a basic but important method that allows us to access elements in the list by index. Although its performance is not as good as ArrayList corresponding method, in some scenarios (such as frequent insertion and deletion)LinkedList is still the better choice. Understanding get() how the method works and its performance characteristics can help us make more reasonable data structure choices in actual development.

Java LinkedList Java LinkedList