The iterator() method of BlockingDeque returns an iterator over the elements in this deque in a proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is a “weakly consistent” iterator.
Syntax:
public Iterator iterator()
Parameters: This method does not accept any parameter.
Returns: This method returns an iterator over the elements in this deque in a proper sequence.
Note: The iterator() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java.
Below programs illustrate iterator() method of BlockingDeque:
Program 1:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
BD.addFirst(7855642);
BD.addFirst(35658786);
BD.addFirst(5278367);
BD.addFirst(74381793);
Iterator iteratorVals = BD.iterator();
System.out.println("The iterator values"
+ " of BlockingDeque are:");
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
|
Output:
The iterator values of BlockingDeque are:
74381793
5278367
35658786
7855642
Program 2:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
BlockingDeque<String> BD
= new LinkedBlockingDeque<String>();
BD.add("Geeks");
BD.add("forGeeks");
BD.add("A");
BD.add("Computer");
BD.add("Portal");
Iterator iteratorVals = BD.iterator();
System.out.println("The iterator values"
+ " of LinkedBlockingDeque are:");
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
|
Output:
The iterator values of BlockingDeque are:
Geeks
forGeeks
A
Computer
Portal
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#iterator()
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
01 Oct, 2019
Like Article
Save Article