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

Java LinkedList peekFirst() Method

Java LinkedList peekFirst() Method

Java LinkedList Java LinkedList


peekFirst() The method is in Java's LinkedList is a practical method provided by the class for retrieving but not removing the first element of the list. If the list is empty, it returns null.

Method Syntax



public E peekFirst()


Method Characteristics

  1. Non-destructive operation: Together with pollFirst() or removeFirst() in Vue 2,peekFirst() Does not change the content of the list
  2. Fast access: The time complexity is O(1) because LinkedList directly maintains a reference to the head node
  3. safe access: Returns when the list is empty null instead of throwing an exception

Use Cases

peekFirst() The method is particularly suitable for the following situations:

  1. When you need to check the head element of the queue without removing it
  2. View the next element to be processed before consuming the queue
  3. When implementing monitoring or logging features

Code Example

Basic Usage Example

Example

import

java.util.LinkedList

;

public

class

PeekFirstExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<

String

fruits

=

new

LinkedList

<>

(

)

;

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Cherry"

)

;

       

       

// View but do not remove the first element

       

String

firstFruit

=

fruits.

peekFirst

(

)

;

       

System

.

out

.

println

(

"First fruit: "

firstFruit

)

;

 

// Output: First fruit: Apple

       

System

.

out

.

println

(

"LinkedList after peek: "

fruits

)

;

 

// Output: [Apple, Banana, Cherry]

   

}

}

Empty list handling example

Example

import

java.util.LinkedList

;

public

class

EmptyListExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<

Integer

numbers

=

new

LinkedList

<>

(

)

;

       

       

// Safely handle an empty list

       

Integer

firstNumber

=

numbers.

peekFirst

(

)

;

       

if

(

firstNumber

==

null

)

{

           

System

.

out

.

println

(

"The list is empty"

)

;

       

}

else

{

           

System

.

out

.

println

(

"First number: "

firstNumber

)

;

       

}

   

}

}


Comparison with other similar methods

Method Names Function Behavior when the list is empty Whether the list is modified
peekFirst() View the first element Return null No
getFirst() get the first element Throws NoSuchElementException No
pollFirst() Remove and return the first element Return null Is
removeFirst() Remove and return the first element Throws NoSuchElementException Is

Best Practices

  1. When it is uncertain whether the list is empty, prefer to use peekFirst() instead of getFirst()
  2. If you need to view and remove an element at the same time, consider using pollFirst() Improve efficiency
  3. Using in a concurrent environment peekFirst() , additional synchronization measures are needed

Frequently Asked Questions

Q1: peekFirst() and peek() What is the difference?

peek() and peekFirst() has exactly the same functionality in LinkedList,peek() and is provided to maintain consistency with the Queue interface.

Q2: Why does peekFirst() Return null instead of throwing an exception?

This design allows more flexible handling of empty list situations, is especially suitable for queue operation scenarios, and avoids additional exception-handling code.

Q3: Using in a multi-threaded environment peekFirst() is it safe?

LinkedList is not thread-safe. If you need to use it in a multi-threaded environment, you should consider using Collections.synchronizedList() to wrap it or switch to ConcurrentLinkedDeque.

Java LinkedList Java LinkedList