Java中的 BlockingDeque iterator() 方法及示例
BlockingDeque的iterator()方法以适当的顺序返回此双端队列中元素的迭代器。元素将按从第一个(头)到最后一个(尾)的顺序返回。返回的迭代器是一个“弱一致”的迭代器。
句法:
public Iterator iterator()
参数:此方法不接受任何参数。
返回:此方法以正确的顺序返回此双端队列中元素的迭代器。
注意: BlockingDeque的 iterator() 方法继承自Java中的 LinkedBlockingDeque 类。
下面的程序说明了BlockingDeque的 iterator() 方法:
方案一:
// Java Program Demonstrate iterator()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of BlockingDeque
BlockingDeque BD
= new LinkedBlockingDeque();
// Add numbers to front of BlockingDeque
BD.addFirst(7855642);
BD.addFirst(35658786);
BD.addFirst(5278367);
BD.addFirst(74381793);
// Call iterator() method of BlockingDeque
Iterator iteratorVals = BD.iterator();
// Print elements of iterator
// created from PriorityBlockingQueue
System.out.println("The iterator values"
+ " of BlockingDeque are:");
// prints the elements using an iterator
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
输出:
The iterator values of BlockingDeque are:
74381793
5278367
35658786
7855642
方案二:
// Java Program Demonstrate iterator()
// method of BlockingDeque
// when list is of strings
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of BlockingDeque
BlockingDeque BD
= new LinkedBlockingDeque();
// Add numbers to front of BlockingDeque
BD.add("Geeks");
BD.add("forGeeks");
BD.add("A");
BD.add("Computer");
BD.add("Portal");
// Call iterator() method of BlockingDeque
Iterator iteratorVals = BD.iterator();
// Print elements of iterator
// created from BlockingQueue
System.out.println("The iterator values"
+ " of LinkedBlockingDeque are:");
// prints the elements using an iterator
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
输出:
The iterator values of BlockingDeque are:
Geeks
forGeeks
A
Computer
Portal
参考: https: Java/util/concurrent/BlockingDeque.html#iterator()