Skip to content
Related Articles

Related Articles

Improve Article

ConcurrentLinkedQueue spliterator() method in Java

  • Last Updated : 26 Nov, 2018
Geek Week

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:






// 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 + ", "));
    }
}
Output:
list of Numbers:
4353, 377139, 624378, 654793, 764764, 838494, 632845,

Example 2:




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

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. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :