Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaConcurrentLinkedQueue spliterator() method in Java

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:




// 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–

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