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-