Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Deque descendingIterator() method in Java

  • Last Updated : 21 Sep, 2018

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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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.




// 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());
        }
    }
}
Output:
The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

Program 2:




// 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());
        }
    }
}
Output:
The iterator values of deque are:
10
15
30
20
5

Program 3: With the help of ArrayDeque.




// 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());
        }
    }
}
Output:
The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

Program 4: With the help of LinkedBlockingDeque.




// 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());
        }
    }
}
Output:
The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

Program 5: With the help of ConcurrentLinkedDeque.




// 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());
        }
    }
}
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–




My Personal Notes arrow_drop_up