Monday, November 18, 2024
Google search engine
HomeLanguagesJavaPriorityBlockingQueue spliterator() method in Java

PriorityBlockingQueue spliterator() method in Java

The spliterator() method of PriorityBlockingQueue returns a Spliterator of the same elements as PriorityBlockingQueue. The returned iterator 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 PriorityBlockingQueue.

Below programs illustrate spliterator() method of PriorityBlockingQueue:

Example 1: Program to demonstrate spliterator() method on PriorityBlockingQueue which contains a list of numbers.




// Java Program Demonstrate spliterator()
// method of PriorityBlockingQueue
  
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
  
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue<Integer> PrioQueue
            = new PriorityBlockingQueue<Integer>(capacityOfQueue);
  
        // Add numbers to PriorityBlockingQueue
        PrioQueue.put(7855642);
        PrioQueue.put(35658786);
        PrioQueue.put(5278367);
        PrioQueue.put(74381793);
        PrioQueue.put(76487590);
        PrioQueue.put(87625142);
  
        // create Spliterator of PrioQueue
        // using spliterator() method
        Spliterator<Integer> numbers = PrioQueue.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Numbers:");
  
        // forEachRemaining method  of Spliterator
        numbers.forEachRemaining((n) -> System.out.println(n));
    }
}


Output:

list of Numbers:
5278367
35658786
7855642
74381793
76487590
87625142

Example 2: Program to demonstrate spliterator() method on PriorityBlockingQueue which contains a list of names.




// Java Program Demonstrate spliterator()
// method of PriorityBlockingQueue
  
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
  
        // create object of PriorityBlockingQueue which contains
        // name of students
        PriorityBlockingQueue<String> names
            = new PriorityBlockingQueue<String>(capacityOfQueue);
  
        // Add names of students of girls college
        names.add("Joyita");
        names.add("Priyanka");
        names.add("Joydeep");
  
        // create Spliterator of PrioQueue
        // using spliterator() method
        Spliterator<String> list = names.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Names:");
  
        // forEachRemaining method  of Spliterator
        list.forEachRemaining((n) -> System.out.println(n));
    }
}


Output:

list of Names:
Joydeep
Priyanka
Joyita

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.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