LinkedTransferQueue iterator() method in Java


The iterator() method of java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to return an iterator over the elements in this queue in proper sequence.

Syntax:

LinkedTransferQueue.iterator()

Return Value: The function returns an iterator over the elements in this queue in proper sequence.

Below programs illustrate the LinkedTransferQueue.iterator() method:

Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate iterator()
// method of LinkedTransferQueue */
  
import java.util.Iterator;
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueueIteratorExample1 {
    public static void main(String[] args)
    {
  
        // Initializing the queue
        LinkedTransferQueue<String>
            queue = new LinkedTransferQueue<String>();
  
        // Adding elements to this queue
        queue.add("Gfg");
        queue.add("is");
        queue.add("fun!!");
  
        // Returns an iterator over the elements
        Iterator<String> iterator = queue.iterator();
  
        // Printing the elements of the queue
        while (iterator.hasNext())
            System.out.print(iterator.next() + " ");
    }
}

chevron_right


Output:

Gfg is fun!!

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate iterator()
// method of LinkedTransferQueue */
  
import java.util.Iterator;
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueueIteratorExample2 {
    public static void main(String[] args)
    {
  
        // Initializing the queue
        LinkedTransferQueue<Integer>
            queue = new LinkedTransferQueue<Integer>();
  
        // Adding elements to this queue
        queue.add(10);
        queue.add(15);
        queue.add(20);
        queue.add(25);
  
        // Returns an iterator over the elements
        Iterator<Integer> iterator = queue.iterator();
  
        // Printing the elements of the queue
        System.out.print("The queue contains ");
        while (iterator.hasNext())
            System.out.print(iterator.next() + " ");
    }
}

chevron_right


Output:

The queue contains 10 15 20 25

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



My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

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.