Java LinkedList descendingIterator() Method
descendingIterator() is a method in Java's LinkedList is a practical method provided by the class that returns a reverse iterator, allowing us to traverse the elements of the list in reverse order (from back to front).
Method Syntax
Iterator
<
E
descendingIterator
(
)descendingIterator() The iterator returned by the method traverses the list elements in reverse order (from the last element to the first element).
Doubly linked list feature
This method takes full advantage of LinkedList the characteristics of being a doubly linked list, enabling efficient traversal starting from the tail.
Fail-fast mechanism
The iterator returned is fail-fast; if the list is modified during iteration (except through the iterator's own remove() method), it throws ConcurrentModificationException.
Use Cases
1. Processing data in reverse
This method is very useful when you need to process data in last-in-first-out (LIFO) order.
2. Stack simulation
can be used to simulate the behavior of a stack, because a stack is a last-in-first-out data structure.
3. Reverse search
In some algorithms, you may need to search for a specific element from back to front.
Code Example
Basic Usage Example
Example
import
java.util.LinkedList
;
import
java.util.Iterator
;
public
class
DescendingIteratorExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a LinkedList
LinkedList
<
String
list
=
new
LinkedList
<>
(
);
// Add elements
list.
add
(
"Apple"
)
;
list.
add
(
"Banana"
)
;
list.
add
(
"Cherry"
)
;
list.
add
(
"Date"
)
;
// Get the reverse iterator
Iterator
<
String
descendingIterator
=list.
descendingIterator
(
)
;
// Traverse using the reverse iterator
System
.
out
.
println
(
"Elements in reverse order:"
)
;
while
(
descendingIterator.
hasNext
(
)
)
{
System
.
out
.
println
(
descendingIterator.
next
(
)
)
;
}
}
}
Output result:
Elements in reverse order:
Date
Cherry
Banana
Apple
Comparison with the regular iterator
Example
import
java.util.LinkedList
;
import
java.util.Iterator
;
public
class
CompareIterators
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
Integer
numbers
=
new
LinkedList
<>
(
);
numbers.
add
(
1
)
;
numbers.
add
(
2
)
;
numbers.
add
(
3
)
;
numbers.
add
(
4
)
;
System
.
out
.
println
(
"Forward iteration:"
)
;
Iterator
<
Integer
forward
=numbers.
iterator
(
)
;
while
(
forward.
hasNext
(
)
)
{
System
.
out
.
println
(
forward.
next
(
)
)
;
}
System
.
out
.
println
(
"\nBackward iteration:"
)
;
Iterator
<
Integer
backward
=numbers.
descendingIterator
(
)
;
while
(
backward.
hasNext
(
)
)
{
System
.
out
.
println
(
backward.
next
(
)
)
;
}
}
}
Output result:
Forward iteration:
1
2
3
4
Backward iteration:
4
3
2
1
Notes
1. Concurrent modification
When using descendingIterator() when, if the list is modified in other ways during iteration (such as directly calling add() or remove() method), it throws ConcurrentModificationException.
2. Performance Considerations
Although LinkedList reverse traversal has very good performance (O(1) time to get each element), but for large lists, memory usage still needs to be considered.
3. The difference from ListIterator
descendingIterator() returns a Iterator interface, while listIterator(size()) can return a ListIterator, which provides more operations (such as add() and set()).
Frequently Asked Questions
Q1: What is the difference between descendingIterator() and the regular iterator()?
A1: The regular iterator() traverses forward starting from the head of the list, while descendingIterator() traverses in reverse starting from the tail of the list.
Q2: What is the time complexity of this method?
A2: Getting the iterator itself is an O(1) operation, and each next() operation is also O(1), because LinkedList is a doubly linked list.
Q3: Can the list be modified during iteration?
A3: Only through the iterator's own remove() method can it be modified; other modification methods will cause ConcurrentModificationException.
Summary
LinkedList 's descendingIterator() method is a simple but powerful tool that provides the ability to traverse the list from back to front. Understanding and correctly using this method can help you handle scenarios requiring reverse data access more efficiently. Remember its fail-fast characteristic, and consider using ListIterator as an alternative.
Java LinkedList