当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LinkedBlockingDeque iterator()用法及代码示例


LinkedBlockingDeque的iterator()方法以适当的顺序在此双端队列中的元素上返回迭代器。元素将按照从头(头)到后(尾)的顺序返回。返回的迭代器是一个弱一致性迭代器。

用法:

public Iterator iterator()

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


返回值:此方法以适当的顺序在此双端队列中的元素上返回迭代器。

以下示例程序旨在说明LinkedBlockingDeque的iterator()方法:

程序1:

// 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<Integer> LBD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // 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

程序2:

// 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<String> LBD 
            = new LinkedBlockingDeque<String>(); 
  
        // 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://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#iterator()



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 LinkedBlockingDeque iterator() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。