LinkedList descendingIterator() Method in Java
In Java, the descendingIterator() method of LinkedList is used to return an iterator that allows to traverse the list in reverse order.
Example 1: This example demonstrates how to use descendingIterator() method with Strings in a LinkedList.
// Java program to use
// descendingIterator()
import java.util.Iterator;
import java.util.LinkedList;
public class Geeks {
public static void main(String[] args) {
// Creating a LinkedList
LinkedList<String> l = new LinkedList<>();
// Adding elements to the LinkedList
l.add("Java");
l.add("Python");
l.add("C++");
// Display the original LinkedList
System.out.println("Original list: " + l);
// Using descendingIterator to
// iterate the list in reverse order
Iterator<String> it = l.descendingIterator();
// Iterating through the list in reverse order
System.out.print("Reverse Order list: ");
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
}
Output
Original list: [Java, Python, C++] Reverse Order list: C++ Python Java
Syntax of LinkedList descendingIterator() Method
Iterator<E> descendingIterator();
Return type: It returns an Iterator that iterates over the elements in the reverse order.
Example 2: This example demonstrates how to use descendingIterator() method with Integers in a LinkedList.
// Use of descendingIterator() with Integers
import java.util.Iterator;
import java.util.LinkedList;
class Geeks {
public static void main(String[] args) {
// Creating an empty LinkedList
LinkedList<Integer> l = new LinkedList<>();
l.add(100);
l.add(200);
l.add(300);
l.add(400);
l.add(500);
// Displaying the original LinkedList
System.out.println("Original list: "
+ l);
// Getting the descending iterator
Iterator<Integer> it
= l.descendingIterator();
// Iterating in reverse order using the
// descendingIterator() method
System.out.print("Iterating in reverse order: ");
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
}
Output
Original list: [100, 200, 300, 400, 500] Iterating in reverse order: 500 400 300 200 100
Example 3: This example demonstrates how to use descendingIterator() method with Objects in a LinkedList.
// Use of descendingIterator() with Object
import java.util.Iterator;
import java.util.LinkedList;
class Human {
int a;
String n;
// Created a Constructor
public Human(int a, String n)
{
this.a = a;
this.n = n;
}
@Override public String toString()
{
return "age=" + a + ", name=" + n;
}
}
public class Geeks {
public static void main(String[] args) {
// Created a Linkedlist to store Human object
LinkedList<Human> l = new LinkedList<>();
// Adding human object to the LinkedList
l.add(new Human(10, "Bob"));
l.add(new Human(28, "Alice"));
l.add(new Human(23, "Robin"));
l.add(new Human(18, "Scott"));
l.add(new Human(34, "David"));
// Getting the descending Iterator
Iterator<Human> it = l.descendingIterator();
System.out.println("Iterating in reverse order:");
// Iterating in reverse order using the
// descendingIterator() method
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Output
Iterating in reverse order: age=34, name=David age=18, name=Scott age=23, name=Robin age=28, name=Alice age=10, name=Bob