Thursday, September 4, 2025
HomeLanguagesJavaLinkedList descendingIterator() method in Java with Examples

LinkedList descendingIterator() method in Java with Examples

The descendingIterator() method of java.util.LinkedList class is used to return an iterator over the elements in this LinkedList in reverse sequential order. The elements will be returned in order from last (tail) to first (head).

Syntax:

public Iterator descendingIterator()

Return Value: This method returns an iterator over the elements in this LinkedList in reverse sequence.

Below are the examples to illustrate the descendingIterator() method

Example 1:




// Java program to demonstrate
// descendingIterator() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of TreeMap<Integer, String>
            LinkedList<String> list = new LinkedList<String>();
  
            // add some elements to list
            list.add("A");
            list.add("B");
            list.add("C");
  
            // print the linked list
            System.out.println("LinkedList:" + list);
  
            // set Iterator as descending
            // using descendingIterator() method
            Iterator x = list.descendingIterator();
  
            // print list with descending order
            while (x.hasNext()) {
                System.out.println("Value is : "
                                   + x.next());
            }
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : "
                               + e);
        }
    }
}


Output:

LinkedList:[A, B, C]
Value is : C
Value is : B
Value is : A

Example 2:




// Java program to demonstrate
// descendingIterator() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of TreeMap<Integer, String>
            LinkedList<Integer>
                list = new LinkedList<Integer>();
  
            // add some elements to list
            list.add(10);
            list.add(20);
            list.add(30);
  
            // print the linked list
            System.out.println("LinkedList:" + list);
  
            // set Iterator as descending
            // using descendingIterator() method
            Iterator x = list.descendingIterator();
  
            // print list with descending order
            while (x.hasNext()) {
                System.out.println("Value is : "
                                   + x.next());
            }
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

LinkedList:[10, 20, 30]
Value is : 30
Value is : 20
Value is : 10
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS