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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。