Java LinkedList descendingIterator() listIterator() Methods Tutorial

In this section, we will learn what the LinkedList descendingIterator() and listIterator() methods are and how to use them in Java.

What is Java LinkedList descendingIterator() Method?

The Java LinkedList descendingIterator() method is used to get an Iterator object from a LinkedList object by which we can iterate through the elements of this object.

You should know that the returned object from this method has the elements of the LinkedList in a reverse order. Basically, from the tail element toward the head.

Note: please check the Iterator interface if you’re not familiar with this interface.

Java descendingIterator() Method Syntax:

public Iterator<E> descendingIterator()

descendingIterator() Method Parameters:

The method does not take an argument.

descendingIterator() Method Return Value:

The return value of this method is an object that implemented the Iterator interface.

descendingIterator() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList descendingIterator() method

import java.util.LinkedList; 
import java.util.Iterator;
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("John");
        list.add("Omid");
        list.add("Jack");
        list.add("Ellen");
        list.add("Elon");

        Iterator<String> it = list.descendingIterator();

        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}

Output:

Elon

Ellen

Jack

Omid

John

What is Java LinkedList listIterator() Method?

The Java LinkedList listIterator() method is used to get a ListIterator object from a LinkedList by which we can iterate through the elements of such an object.

Note: please check the ListIterator section if you’re not familiar with this interface.

Java listIterator() Method Syntax:

public ListIterator<E> listIterator(int index)

listIterator() Method Parameters:

The method takes one argument and that is an index number where we want the iteration to start from.

Note: this argument is optional and, if ignored, the entire elements of the target LinkedList object will be in the returned iterator object.

listIterator() Method Return Value:

The return value of this method is an object that implemented the ListIterator interface.

listIterator() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList listIterator() method

import java.util.LinkedList; 
import java.util.ListIterator;
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("John");
        list.add("Omid");
        list.add("Jack");
        list.add("Ellen");
        list.add("Elon");

        ListIterator<String> it = list.listIterator();

        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}

Output:

John

Omid

Jack

Ellen

Elon
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies