Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaConcurrentLinkedQueue iterator() method in Java

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:




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


Output:

ConcurrentLinkedQueue :
[Aman, Amar, Sanjeet, Rabi]

The String Values of iterator are:
Aman
Amar
Sanjeet
Rabi

Example 2:




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


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–

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments