Java LinkedList descendingIterator() Method
Example
Loop through a list in reverse order:
import java.util.LinkedList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
Iterator<String> it = cars.descendingIterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Definition and Usage
The descendingIterator() method returns an iterator (an object that lets you loop through a collection). The iterator starts at the last item of the list and moves toward the first item.
The method does not change the list.
Tip: Use the iterator() method to loop through the list from the first item to the last.
Syntax
public Iterator<T> descendingIterator()
T refers to the data type of items in the list.
Parameter Values
None.
Technical Details
| Returns: | An Iterator that goes through the items of the list from the last item to the first. |
|---|
Related Pages
❮ LinkedList Methods