Wednesday, July 3, 2024
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()

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments