Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaLinkedTransferQueue iterator() method in Java

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:




// 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()

RELATED ARTICLES

Most Popular

Recent Comments