📜  Java中的 ArrayBlockingQueue spliterator() 方法

📅  最后修改于: 2022-05-13 01:55:50.574000             🧑  作者: Mango

Java中的 ArrayBlockingQueue spliterator() 方法

ArrayBlockingQueue 的spliterator()方法在ArrayBlockingQueue的元素上返回一个 Spliterator。返回的迭代器是弱一致的。 Spliterator 可以与Java 8 中的 Streams 一起使用。Spliterator 也可以单独和批量遍历元素。
句法:

public Spliterator spliterator()

返回值:此方法返回 ArrayBlockingQueue 中元素的 Spliterator。
下面的程序说明了 ArrayBlockingQueue 类的 spliterator() 方法:
方案一:

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 Queue
            = new ArrayBlockingQueue(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 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));
    }
}


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 QueueOfStrings
            = new ArrayBlockingQueue(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
            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);
    }
}


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

方案二:

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 QueueOfStrings
            = new ArrayBlockingQueue(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
            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);
    }
}
输出:
Size of Spliterator : 6
list of Country names:
Value = India
Value = Pakistan
Value = England
Value = China
Value = UAE
Value = Spain

参考: https: Java/util/concurrent/ArrayBlockingQueue.html#spliterator–