Skip to content
Related Articles

Related Articles

LinkedTransferQueue iterator() method in Java
  • Last Updated : 14 Sep, 2018


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:






// 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() + " ");
    }
}


Output:

Gfg is fun!!

Program 2:




// 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() + " ");
    }
}


Output:

The queue contains 10 15 20 25

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :