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


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


Java.util.concurrent.ConcurrentLinkedDeque.iterator()方法用於返回與ConcurrentLinkedDeque元素相同的元素的迭代器。從雙端隊列中存在的元素以隨機順序返回元素。

用法:

Iterator iterate_value = ConcurrentLinkedDeque.iterator();

參數:該函數不帶任何參數。


返回值:該方法遍曆雙端隊列的元素並返回值(迭代器)。

以下示例程序旨在說明Java.util.concurrent.ConcurrentLinkedDeque.iterator()方法的使用:

示例1:

// Java code to illustrate iterator() 
  
import java.util.concurrent.*; 
import java.util.*; 
  
public class ConcurrentLinkedDequeDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty ConcurrentLinkedDeque 
        ConcurrentLinkedDeque<String> deque 
            = new ConcurrentLinkedDeque<String>(); 
  
        // Use add() method to add elements 
        // into the ConcurrentLinkedDeque 
        deque.add("Welcome"); 
        deque.add("To"); 
        deque.add("Geeks"); 
        deque.add("4"); 
        deque.add("Geeks"); 
  
        // Displaying the ConcurrentLinkedDeque 
        System.out.println("ConcurrentLinkedDeque: "
                           + deque); 
  
        // Creating an iterator 
        Iterator value = deque.iterator(); 
  
        // Displaying the values 
        // after iterating through the deque 
        System.out.println("The iterator values are: "); 
        while (value.hasNext()) { 
            System.out.println(value.next()); 
        } 
    } 
}
輸出:
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
The iterator values are: 
Welcome
To
Geeks
4
Geeks

示例2:具有整數元素的ConcurrentLinkedDeque。

// Java code to illustrate iterator() 
  
import java.util.concurrent.*; 
import java.util.*; 
  
public class ConcurrentLinkedDequeDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty ConcurrentLinkedDeque 
        ConcurrentLinkedDeque<Integer> deque 
            = new ConcurrentLinkedDeque<Integer>(); 
  
        // Use add() method 
        // to add elements into the ConcurrentLinkedDeque 
        deque.add(10); 
        deque.add(20); 
        deque.add(30); 
        deque.add(40); 
        deque.add(50); 
  
        // Displaying the ConcurrentLinkedDeque 
        System.out.println("ConcurrentLinkedDeque: "
                           + deque); 
  
        // Creating an iterator 
        Iterator value = deque.iterator(); 
  
        // Displaying the values 
        // after iterating through the deque 
        System.out.println("The iterator values are: "); 
        while (value.hasNext()) { 
            System.out.println(value.next()); 
        } 
    } 
}
輸出:
ConcurrentLinkedDeque: [10, 20, 30, 40, 50]
The iterator values are: 
10
20
30
40
50


相關用法


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