Deque descendingIterator() method in Java

The descendingIterator(E e) method of Deque Interface returns an iterator over the elements in this deque in a reverse sequential order. The elements will be returned in order from last(tail) to first(head). The returned iterator is a “weakly consistent” iterator.

Syntax:

Iterator descendingIterator()

Parameters: This method does not accept any parameter.

Return Value: This method returns an iterator over the elements in this deque in a proper sequence

Below programs illustrate the descendingIterator() method in Java:



Program 1: With the help of LinkedList.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate descendingIterator()
// method of Deque in Java
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque<String> de_que = new LinkedList<String>();
  
        // Use add() method to add elements into the Queue
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Call iterator() method of deque
        Iterator iteratorVals = de_que.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate descendingIterator()
// method of Deque in Java
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque<Integer> de_que = new LinkedList<Integer>();
  
        // Use add() method to add elements into the Queue
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
  
        // Call iterator() method of deque
        Iterator iteratorVals = de_que.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of deque are:
10
15
30
20
5

Program 3: With the help of ArrayDeque.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate descendingIterator()
// method of Deque in Java
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque<String> de_que = new ArrayDeque<String>();
  
        // Use add() method to add elements into the Queue
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Call iterator() method of deque
        Iterator iteratorVals = de_que.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

Program 4: With the help of LinkedBlockingDeque.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate descendingIterator()
// method of Deque in Java
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque<String> de_que = new LinkedBlockingDeque<String>();
  
        // Use add() method to add elements into the Queue
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Call iterator() method of deque
        Iterator iteratorVals = de_que.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

Program 5: With the help of ConcurrentLinkedDeque.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate descendingIterator()
// method of Deque in Java
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque<String> de_que = new ConcurrentLinkedDeque<String>();
  
        // Use add() method to add elements into the Queue
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Call iterator() method of deque
        Iterator iteratorVals = de_que.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#descendingIterator–

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.