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 deque
= new ConcurrentLinkedDeque();
// 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 deque
= new ConcurrentLinkedDeque();
// 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