ArrayBlockingQueue spliterator() method in Java

The spliterator() method of ArrayBlockingQueue return a Spliterator on the elements of ArrayBlockingQueue. The returned iterator is weakly consistent. Spliterator can be used with Streams in Java 8. Spliterator can traverse elements individually and in bulk too.

Syntax:

public Spliterator spliterator()

Return Value: This method returns a Spliterator over the elements in ArrayBlockingQueue.



Below programs illustrates spliterator() method of ArrayBlockingQueue class:

Praogram 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of ArrayBlockingQueue
  
import java.util.concurrent.ArrayBlockingQueue;
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of ArrayBlockingQueue
        int capacityOfQueue = 7;
  
        // create object of ArrayBlockingQueue
        ArrayBlockingQueue<Integer> Queue
            = new ArrayBlockingQueue<Integer>(capacityOfQueue);
  
        // Add element to ArrayBlockingQueue
        Queue.add(22);
        Queue.add(34);
        Queue.add(45);
        Queue.add(67);
  
        // create Spliterator of Queue
        // using spliterator() method
        Spliterator<Integer> numbers = Queue.spliterator();
  
        // getExactSize of Spliterator
        System.out.println("Size of Spliterator : "
                           + numbers.estimateSize());
  
        System.out.println("list of Numbers:");
        // forEachRemaining method of Spliterator
        numbers.forEachRemaining((n) -> System.out.println(n));
    }
}

chevron_right


Output:

Size of Spliterator : 4
list of Numbers:
22
34
45
67

Praogram 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of ArrayBlockingQueue
  
import java.util.concurrent.ArrayBlockingQueue;
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of ArrayBlockingQueue
        int capacityOfQueue = 7;
  
        // create object of ArrayBlockingQueue
        ArrayBlockingQueue<String> QueueOfStrings
            = new ArrayBlockingQueue<String>(capacityOfQueue);
  
        // Add element to ArrayBlockingQueue
        QueueOfStrings.add("India");
        QueueOfStrings.add("Pakistan");
        QueueOfStrings.add("England");
        QueueOfStrings.add("China");
        QueueOfStrings.add("UAE");
        QueueOfStrings.add("Spain");
  
        // create Spliterator of QueueOfStrings
        // using spliterator() method
        Spliterator<String>
            listOfStrings = QueueOfStrings.spliterator();
  
        // getExactSize of Spliterator
        System.out.println("Size of Spliterator : "
                           + listOfStrings.estimateSize());
  
        System.out.println("list of Country names:");
  
        // forEachRemaining method of Spliterator
        listOfStrings.forEachRemaining((str) -> print(str));
    }
    public static void print(String str)
    {
        System.out.println("Value = " + str);
    }
}

chevron_right


Output:

Size of Spliterator : 6
list of Country names:
Value = India
Value = Pakistan
Value = England
Value = China
Value = UAE
Value = Spain

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