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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。