Java中的 PriorityBlockingQueue spliterator() 方法
PriorityBlockingQueue 的spliterator()方法返回与PriorityBlockingQueue相同元素的Spliterator 。返回的迭代器是弱一致的。它可以与Java 8 中的 Streams 一起使用。它也可以单独和批量遍历元素。Spliterator 是遍历元素的更好方法,因为它提供了对元素的更多控制。
句法:
public Spliterator spliterator()
返回:此方法在 PriorityBlockingQueue 中的元素上返回一个 Spliterator。
下面的程序说明了 PriorityBlockingQueue 的 spliterator() 方法:
示例 1:在包含数字列表的 PriorityBlockingQueue 上演示 spliterator() 方法的程序。
// 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 PrioQueue
= new PriorityBlockingQueue(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 numbers = PrioQueue.spliterator();
// print result from Spliterator
System.out.println("list of Numbers:");
// forEachRemaining method of Spliterator
numbers.forEachRemaining((n) -> System.out.println(n));
}
}
输出:
list of Numbers:
5278367
35658786
7855642
74381793
76487590
87625142
示例 2:在包含名称列表的 PriorityBlockingQueue 上演示 spliterator() 方法的程序。
// 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 names
= new PriorityBlockingQueue(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 list = names.spliterator();
// print result from Spliterator
System.out.println("list of Names:");
// forEachRemaining method of Spliterator
list.forEachRemaining((n) -> System.out.println(n));
}
}
输出:
list of Names:
Joydeep
Priyanka
Joyita
参考: https: Java/util/concurrent/PriorityBlockingQueue.html#spliterator–