LinkedBlockingDeque iterator() method in Java

The iterator() method of LinkedBlockingDeque 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.

Below programs illustrate iterator() method of LinkedBlockingDeque:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate iterator()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
  
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to front of LinkedBlockingDeque
        LBD.addFirst(7855642);
        LBD.addFirst(35658786);
        LBD.addFirst(5278367);
        LBD.addFirst(74381793);
  
        // Call iterator() method of LinkedBlockingDeque
        Iterator iteratorVals = LBD.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of LinkedBlockingDeque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of LinkedBlockingDeque are:
74381793
5278367
35658786
7855642

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate iterator()
// method of LinkedBlockingDeque
// when list is of strings
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
  
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<String> LBD
            = new LinkedBlockingDeque<String>();
  
        // Add numbers to front of LinkedBlockingDeque
        LBD.add("Geeks");
        LBD.add("forGeeks");
        LBD.add("A");
        LBD.add("Computer");
        LBD.add("Portal");
  
        // Call iterator() method of LinkedBlockingDeque
        Iterator iteratorVals = LBD.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of LinkedBlockingDeque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of LinkedBlockingDeque are:
Geeks
forGeeks
A
Computer
Portal

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#iterator()



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.