In this method the elements of underlying deque are returned by an iterator sequentially in reverse order i.e. from tail to head.
descendingIterator() method of Deque Interface in Java.
In this section we will discuss how can descendingIterator() method be implemented in Deque interface in java.
Syntax
Iterator<E> descendingIterator()
This method returns the element of deque in reverse order.
In this method the elements of underlying deque are returned by an iterator sequentially in reverse order i.e. from tail to head.
Parameter description
This method has no parameter but, it returns an iterator over the elements in the underlying deque in sequence in reverse order.
Example of descendingIterator() method
In this example we will show how does descendingIterator() method work in Deque interface. In the following example we will make a LinkedList type Deque and inserted the element into this deque using add( e) method finally, we will display elements of this deque in reverse order using descendingIterator() method with some more implementation of Iterator interface methods such as hasNext() and next().
Example :
package devmanuals.com; import java.util.Deque; import java.util.LinkedList; import java.util.Iterator; public class DequeDescendingIterator { public static void main(String args[]) { Deque<String> dq = new LinkedList<String>(); dq.add("15"); dq.add("16"); dq.add("17"); dq.add("18"); dq.add("19"); dq.add("20"); System.out.println("Elements of deque : " + dq); System.out.println("Elements of deque in reverse order :"); Iterator<String> it =dq.descendingIterator(); while (it.hasNext()) System.out.println(it.next()); } }
Output :
Elements of deque : [15, 16, 17, 18, 19, 20] Elements of deque in reverse order : 20 19 18 17 16 15 |
[ 0 ] Comments