Java LinkedList listIterator() Method
listIterator() The method is in Java's LinkedList is an important method provided by the class; it returns a ListIterator object used to traverse the elements in the list. Unlike the regular Iterator Compared toListIterator provides richer operation capabilities.
Method Syntax
public
ListIterator
<
E
listIterator
(
)
public
ListIterator
<
E
listIterator
(
int
index
)index parameter
- Type:
int - Meaning: The position index where the iterator starts
- Valid range:
0 <= index <= size() - If the passed index equals the list size (
size()), the iterator will point to the end of the list
Return Value
Returns a ListIterator object, which provides the ability to traverse the list bidirectionally.
Main methods of ListIterator
Traversal methods
boolean hasNext(): Checks whether there is a next elementE next(): Returns the next element and advances the cursorboolean hasPrevious(): Checks whether there is a previous elementE previous(): Returns the previous element and moves the cursor backward
Modification methods
void add(E e): Inserts an element at the current positionvoid remove(): Removes the element last returned bynext()orprevious()the returned elementvoid set(E e): Replaces the element last returned bynext()orprevious()the returned element
Usage examples
Basic traversal example
Example
import
java.util.LinkedList
;
import
java.util.ListIterator
;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
fruits
=
new
LinkedList
<>
(
);
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Cherry"
)
;
// Get the ListIterator
ListIterator
<
String
iterator
=fruits.
listIterator
(
)
;
// Forward traversal
System
.
out
.
println
(
"Forward traversal:"
)
;
while
(
iterator.
hasNext
(
)
)
{
System
.
out
.
println
(
iterator.
next
(
)
)
;
}
// Reverse traversal
System
.
out
.
println
(
"\nReverse traversal:"
)
;
while
(
iterator.
hasPrevious
(
)
)
{
System
.
out
.
println
(
iterator.
previous
(
)
)
;
}
}
}
Traversal starting from a specified position
Example
ListIterator
<
String
iterator
=fruits.
listIterator
(
1
)
;
// Start from index 1
System
.
out
.
println
(
"Traversal starting from index 1:"
)
;
while
(
iterator.
hasNext
(
)
)
{
System
.
out
.
println
(
iterator.
next
(
)
)
;
}
Modify list content
Example
ListIterator
<
String
iterator
=fruits.
listIterator
(
)
;
iterator.
next
(
)
;
// Move to the first element
iterator.
set
(
"Apricot"
)
;
// Modify the first element
iterator.
add
(
"Blueberry"
)
;
// Add a new element at the current position
System
.
out
.
println
(
"Modified list:"
)
;
for
(
String
fruit
:
fruits
)
{
System
.
out
.
println
(
fruit
)
;
}
Notes
-
Concurrent Modification: If the list is modified in other ways during iteration (rather than through the iterator's own methods), it will throw
ConcurrentModificationExceptionexception. -
Initial position: The newly created
ListIteratorcursor's initial position is before the first element (for the no-argument version) or before the specified index position (for the parameterized version). -
bounds checking: Calling
next()orprevious(), if there is no corresponding element, it throwsNoSuchElementException. -
Modification operation restrictions: When calling
add(),remove()orset()before, you must first callnext()orprevious(), otherwise it will throwIllegalStateException.
Difference from Iterator
| Feature | Iterator | ListIterator |
|---|---|---|
| Traversal direction | One-way (forward only) | Bidirectional (forward and backward) |
| Modification methods | Only remove() | add(), remove(), set() |
| Get index | Not supported | Supports nextIndex() and previousIndex() |
| Creation method | iterator() | listIterator() |
Performance considerations
LinkedList'slistIterator()method has a time complexity of O(1) because it only creates a new iterator object.- The various operations of the iterator itself (such as
next(),previous()) have a time complexity of O(1) becauseLinkedListis implemented based on a doubly linked list. - For scenarios that require frequent insertion or deletion in the middle of a list, using
ListIteratoris more efficient than operating directly through the index.
Summary
LinkedList 's listIterator() method provides more powerful functionality than the regular iterator, and is especially suitable for scenarios that require modifying list content during traversal or need bidirectional traversal. Understanding and proficiently using this method allows you to operate LinkedList.
Java LinkedList