Java LinkedList descendingIterator() Method with Examples

In the previous article, we have discussed about Java LinkedList iterator() Method with Examples

In this article we are going to see the use of Java LinkedList descendingIterator() method along with suitable examples.

Java LinkedList descendingIterator() Method with Examples

This java.util.LinkedList.descendingIterator() method is used to get an Iterator to retrieve each elements of the LinkedList in a descending order. Here descending order means in reverse order where all the elements will be returned from tail element to head element of LinkedList.

Syntax:

LinkedListName.descendingIterator()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see different examples to understand it more clearly.

Example-1: Java LinkedList  descendingIterator() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Create an iterator object of descendingIterator and iterate each object from last to first by using inbuilt hasNext() and next() method. Where hasNext() method checks next elements present or not and next() method returns the next element if it is present.
  • Finally get the elements of LinkedList in reverse order.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an LinkedList of string datatype
        LinkedList<String> l1 = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l1.add("vivo");
        l1.add("htc");
        l1.add("samsung");
        l1.add("realme");
        l1.add("nokia");
        // Prints the LinkedList elements
        System.out.println("The elements in the LinkedList are: "+l1);
        // Create an iterator object 
        Iterator<String> iterate = l1.descendingIterator();
        System.out.print("LinkedList: ");
        // hasNext() method on iterator object to check next element exists or not
        // means loop will continue till covering all elements
        while(iterate.hasNext())
        {
            // by using next() method get the next element and print it
            System.out.print(iterate.next());
            System.out.print("  ");
        }
    }
}
Output:

The elements in the LinkedList are: [vivo, htc, samsung, realme, nokia]
LinkedList: nokia realme samsung htc vivo

Example-2: Java LinkedList  descendingIterator() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Create an iterator object of descendingIterator and iterate each object from last to first by using inbuilt hasNext() and next() method. Where hasNext() method checks next elements present or not and next() method returns the next element if it is present.
  • Finally get the elements of LinkedList in reverse order.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {  
        // Create an LinkedList of Integer datatype
        LinkedList<Integer> l1 = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l1.add(1);
        l1.add(100);
        l1.add(84);
        l1.add(17);
        l1.add(0);
        // Prints the LinkedList elements
        System.out.println("The elements in the LinkedList are: "+l1);
        // Create an iterator object of 
        Iterator<Integer> iterate = l1. descendingIterator();
        System.out.print("LinkedList: ");
        // hasNext() method on iterator object to check next element exists or not
        // means loop will continue till covering all elements
        while(iterate.hasNext())
        {
            // by using next() method get the next element and print it
            System.out.print(iterate.next());
            System.out.print("  ");
        }
    }
}

Output:

The elements in the LinkedList are: [1, 100, 84, 17, 0]
LinkedList: 0 17 84 100 1

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Related Java Programs: