📌  相关文章
📜  Java中的LinkedBlockingDeque iterator()方法

📅  最后修改于: 2022-05-13 01:54:41.757000             🧑  作者: Mango

Java中的LinkedBlockingDeque iterator()方法

LinkedBlockingDequeiterator()方法以适当的顺序返回此双端队列中元素的迭代器。元素将按从第一个(头)到最后一个(尾)的顺序返回。返回的迭代器是一个“弱一致”的迭代器。

句法:

public Iterator iterator()

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

返回:此方法以正确的顺序返回此双端队列中元素的迭代器。

下面的程序说明了LinkedBlockingDeque的 iterator() 方法:

方案一:

// Java Program Demonstrate iterator()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
  
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to front of LinkedBlockingDeque
        LBD.addFirst(7855642);
        LBD.addFirst(35658786);
        LBD.addFirst(5278367);
        LBD.addFirst(74381793);
  
        // Call iterator() method of LinkedBlockingDeque
        Iterator iteratorVals = LBD.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        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 LinkedBlockingDeque are:
74381793
5278367
35658786
7855642

方案二:

// Java Program Demonstrate iterator()
// method of LinkedBlockingDeque
// when list is of strings
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
  
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to front of LinkedBlockingDeque
        LBD.add("Geeks");
        LBD.add("forGeeks");
        LBD.add("A");
        LBD.add("Computer");
        LBD.add("Portal");
  
        // Call iterator() method of LinkedBlockingDeque
        Iterator iteratorVals = LBD.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        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 LinkedBlockingDeque are:
Geeks
forGeeks
A
Computer
Portal

参考: https: Java/util/concurrent/LinkedBlockingDeque.html#iterator()