當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BlockingDeque iterator()用法及代碼示例


BlockingDeque的iterator()方法以適當的順序在此雙端隊列中的元素上返回迭代器。元素將按照從頭(頭)到後(尾)的順序返回。返回的迭代器是一個弱一致性迭代器。

用法:

public Iterator iterator()

參數:此方法不接受任何參數。


返回值:此方法以適當的順序在此雙端隊列中的元素上返回迭代器。

注意:BlockingDeque的iterator()方法已從Java中的LinkedBlockingDeque類繼承。

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

程序1:

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

程序2:

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



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 BlockingDeque iterator() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。