Java Utility Library

Java LinkedList - descendingIterator() Method



The java.util.LinkedList.descendingIterator() method returns an iterator over the elements in the list in reverse sequential order. The elements will be returned in order from last (tail) to first (head) element.

Syntax

public Iterator<E> descendingIterator()

Here, E is the type of element maintained by the container.


Parameters

No parameter is required.

Return Value

Returns an iterator over the elements in the list in reverse sequential order.

Exception

NA

Example:

In the example below, the java.util.LinkedList.descendingIterator() method returns an iterator over the elements in the given list in reverse sequential order. It is further used to display the content of the list.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a linkedlist
    LinkedList<Integer> MyList = new LinkedList<Integer>();

    //populating linkedlist
    MyList.add(10);
    MyList.add(20);
    MyList.add(30);
    MyList.add(40);

    //create a reverse iterator
    Iterator it = MyList.descendingIterator();

    //printing linkedlist
    System.out.print("MyList contains: "); 
    while(it.hasNext()) 
      System.out.print(it.next()+ " ");  
  }
}

The output of the above code will be:

MyList contains: 40 30 20 10 

❮ Java.util - LinkedList