Site icon JavaGoal

descendingIterator() method

descendingIterator() method: The Descending Iterator in Java method of LinkedList class employs to return an iterator over the elements in this LinkedList in reverse sequential order. The descendingIterator returns an iterator with elements and these elements will be returned in order from last to first. It returns the object of the Iterator.

Iterator descendingIterator();
import java.util.Iterator;
import java.util.LinkedList;

public class ExampleOfLinkedList {

	public static void main(String[] args) 
	{
		LinkedList<String> listOfNames = new LinkedList<String>();
		
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("RAVI");
		
		Iterator<String> iterator = listOfNames.descendingIterator();
		while(iterator.hasNext())
		{
			System.out.println("List from Name = "+iterator.next());
		}
          }
}

Output: List from Name = RAVI
List from Name = GOAL
List from Name = JAVA

Exit mobile version