PriorityBlockingQueue的spliterator()方法返回与PriorityBlockingQueue相同的元素的分隔符。返回的迭代器是弱一致性的。它可以与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<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));
}
}
输出:
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<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));
}
}
输出:
list of Names: Joydeep Priyanka Joyita
相关用法
- Java ArrayList spliterator()用法及代码示例
- Java CopyOnWriteArraySet spliterator()用法及代码示例
- Java CopyOnWriteArrayList spliterator()用法及代码示例
- Java ConcurrentSkipListSet spliterator()用法及代码示例
- Java LinkedList spliterator()用法及代码示例
- Java LinkedTransferQueue spliterator()用法及代码示例
- Java ArrayDeque spliterator()用法及代码示例
- Java ConcurrentLinkedQueue spliterator()用法及代码示例
- Java HashSet spliterator()用法及代码示例
- Java ArrayBlockingQueue spliterator()用法及代码示例
- Java PriorityQueue spliterator()用法及代码示例
- Java LinkedBlockingDeque spliterator()用法及代码示例
- Java 8 LinkedBlockingQueue spliterator()用法及代码示例
- Java ConcurrentLinkedDeque Spliterator()用法及代码示例
- Java PriorityBlockingQueue contains()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 PriorityBlockingQueue spliterator() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。