Deque接口的DescendingIterator(E e)方法以相反的顺序在此双端队列中的元素上返回一个迭代器。元素将按从last(tail)到first(head)的顺序返回。返回的迭代器是一个弱一致性迭代器。
用法:
Iterator descendingIterator()
参数:此方法不接受任何参数。
返回值:此方法以适当的顺序返回此双端队列中的元素的迭代器
以下程序说明了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-
相关用法
- Java NavigableSet descendingIterator()用法及代码示例
- Java ConcurrentLinkedDeque descendingIterator()用法及代码示例
- Java LinkedBlockingDeque descendingIterator()用法及代码示例
- Java ArrayDeque descendingIterator()用法及代码示例
- Java ConcurrentSkipListSet descendingIterator()用法及代码示例
- Java TreeSet descendingIterator()用法及代码示例
- Java LinkedList descendingIterator()用法及代码示例
- Java Deque contains()用法及代码示例
- Java Deque add()用法及代码示例
- Java Deque getLast()用法及代码示例
- Java Deque getFirst()用法及代码示例
- Java Deque offerLast()用法及代码示例
- Java Deque iterator()用法及代码示例
- Java Deque element()用法及代码示例
- Java Deque offerFirst()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Deque descendingIterator() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。