Open In App

ArrayBlockingQueue spliterator() method in Java

Last Updated : 13 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:
Program 1: 

Java




// 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));
    }
}


Output: 

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

 

Program 2:

Java




// 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);
    }
}


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–
 



Previous Article
Next Article

Similar Reads

Java 8 | LinkedBlockingQueue spliterator() method with Examples
The spliterator() method of LinkedBlockingQueue returns a Spliterator of the same elements as LinkedBlockingQueue. 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.Syntax: public Spliterator spliterator() Return Value: This method returns a Spliterator over
3 min read
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 el
2 min read
LinkedTransferQueue spliterator() method in Java
The java.util.concurrent.LinkedTransferQueue.spliterator() method is an in-built function in Java which returns a weakly uniform Spliterator across the elements of this queue. Syntax: LinkedTransferQueue.spliterator() Parameters: The function does not accept any parameter. Return Value: The function returns a Spliterator across the elements of this
2 min read
ArrayList spliterator() method in Java
The spliterator() method of ArrayList returns a Spliterator of the same elements as ArrayList but created Spliterator is late-binding and fail-fast. A late-binding Spliterator binds to the source of elements. It means that Arraylist at the point of the first traversal, first split, or the first query for estimated size, rather than at the time the
3 min read
LinkedBlockingDeque spliterator() method in Java
The spliterator() method of LinkedBlockingDeque returns a Spliterator on the elements of LinkedBlockingDeque. 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() Returns: This method returns a Spliterator
2 min read
LinkedList spliterator() method in Java
The spliterator() method of LinkedList returns a Spliterator which is late-binding and fail-fast with the same elements as LinkedList. A late-binding Spliterator binds to the source of elements means LinkedList at the point of first traversal, first split, or first query for estimated size, rather than at the time the Spliterator is created. It can
3 min read
HashSet spliterator() method in Java
The spliterator() method of HashSet returns a Spliterator with the same elements as HashSet. The returned Spliterator is late-binding and fail-fast Spliterator. A late-binding Spliterator binds to the source of elements means HashSet at the point of first traversal, first split, or first query for estimated size, rather than at the time the Spliter
2 min read
ArrayDeque spliterator() method in Java
The spliterator() method of ArrayDeque returns a Spliterator of the same elements as ArrayDeque but created Spliterator is late-binding and fail-fast. A late-binding Spliterator binds to the source of elements means ArrayDeque at the point of first traversal, first split, or first query for estimated size, rather than at the time the Spliterator is
2 min read
ConcurrentSkipListSet spliterator() method in Java
The java.util.concurrent.ConcurrentSkipListSet.spliterator() method is an in-built function in Java which returns a weakly uniform Spliterator across the elements of this set. Syntax: ConcurrentSkipListSet.spliterator() Parameters: The function does not accept any parameter. Return Value: The function returns a Spliterator across the elements of th
2 min read
PriorityQueue spliterator() method in Java
The spliterator() method of PriorityQueue returns a Spliterator the same elements as PriorityQueue.The returned Spliterator is late-binding and fail-fast Spliterator. A late-binding Spliterator binds to the source of elements means PriorityQueue at the point of first traversal, first split, or first query for estimated size, rather than at the time
2 min read