Java Deque descendingIterator()方法

Java Deque descendingIterator()方法

Deque接口的 descendingIterator(E e) 方法以相反的顺序返回该deque中元素的迭代器。这些元素将按照从最后(尾部)到第一个(头部)的顺序返回。返回的迭代器是一个 “弱一致性 “迭代器。

语法

Iterator descendingIterator()

参数: 该方法不接受任何参数。

返回值: 该方法以适当的顺序返回该deque中的元素的迭代器。

以下程序说明了Java中的descendingIterator()方法。

程序1: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());
        }
    }
}

输出:

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

程序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());
        }
    }
}

输出:

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

程序3: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());
        }
    }
}

输出:

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

程序4: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());
        }
    }
}

输出:

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

程序5: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());
        }
    }
}

输出:

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

参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#descendingIterator-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程