ConcurrentLinkedQueue iterator() method in Java

The iterator() method of ConcurrentLinkedQueue is used to returns an iterator of the same elements as this ConcurrentLinkedQueue in a proper sequence. The elements returned from this method contains elements in order from first(head) to last(tail). The returned iterator is weakly consistent.

Syntax:

public Iterator iterator()

Returns: This method returns the iterator having same elements as present in ConcurrentLinkedQueue in Proper Sequence.



Below programs illustrate iterator() method of ConcurrentLinkedQueue:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate iterator()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue<String>
            queue = new ConcurrentLinkedQueue<String>();
  
        // Add String to queue
        queue.add("Aman");
        queue.add("Amar");
        queue.add("Sanjeet");
        queue.add("Rabi");
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue :\n" + queue);
  
        // Call iterator() method
        Iterator iterator = queue.iterator();
  
        // Print elements of iterator
        System.out.println("\nThe String Values of iterator are:");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

chevron_right


Output:

ConcurrentLinkedQueue :
[Aman, Amar, Sanjeet, Rabi]

The String Values of iterator are:
Aman
Amar
Sanjeet
Rabi

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate iterator()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue<Integer>
            queue = new ConcurrentLinkedQueue<Integer>();
  
        // Add Numbers to queue
        queue.add(4353);
        queue.add(7824);
        queue.add(78249);
        queue.add(8724);
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
  
        // Call iterator() method
        Iterator values = queue.iterator();
  
        // Print elements of iterator
        System.out.println("\nThe Numbers of iterator are:");
        while (values.hasNext()) {
            System.out.println(values.next());
        }
    }
}

chevron_right


Output:

ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]

The Numbers of iterator are:
4353
7824
78249
8724

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#iterator–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.