Wednesday, July 3, 2024
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–

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments