ConcurrentLinkedQueue spliterator() method in Java

The spliterator() method of ConcurrentLinkedQueue is used to get a Spliterator of the same elements as ConcurrentLinkedQueue. Created Spliterator is weakly consistent. It can be used with Streams in Java 8. Also it can traverse elements individually and in bulk too. Spliterator is better way to traverse over element because it provides more control on elements.

Syntax:

public Spliterator spliterator()

Returns: This method returns a Spliterator over the elements in ConcurrentLinkedQueue.



Below programs illustrate spliterator() method of ConcurrentLinkedQueue:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// 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(377139);
        queue.add(624378);
        queue.add(654793);
        queue.add(764764);
        queue.add(838494);
        queue.add(632845);
  
        // create Spliterator of ConcurrentLinkedQueue
        // using spliterator() method
        Spliterator<Integer> spt = queue.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Numbers:");
  
        // forEachRemaining method of Spliterator
        spt.forEachRemaining((n) -> System.out.print(n + ", "));
    }
}

chevron_right


Output:

list of Numbers:
4353, 377139, 624378, 654793, 764764, 838494, 632845,

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// 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");
        queue.add("Debasis");
        queue.add("Raunak");
        queue.add("Mahesh");
  
        // create Spliterator of ConcurrentLinkedQueue
        // using spliterator() method
        Spliterator<String> spt = queue.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Strings:");
  
        // forEachRemaining method of Spliterator
        spt.forEachRemaining((n) -> System.out.print(n + ", "));
    }
}

chevron_right


Output:

list of Strings:
Aman, Amar, Sanjeet, Rabi, Debasis, Raunak, Mahesh,

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#spliterator–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.