當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ArrayBlockingQueue spliterator()用法及代碼示例


ArrayBlockingQueue的spliterator()方法在ArrayBlockingQueue的元素上返回一個分隔符。返回的迭代器是弱一致性的。 Spliterator可以與Java 8中的Streams一起使用。Spliterator也可以單獨和批量遍曆元素。

用法:

public Spliterator spliterator()

返回值:此方法在ArrayBlockingQueue中的元素上返回一個Spliterator。


以下示例程序旨在說明ArrayBlockingQueue類的spliterator()方法:

實績1:

// 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)); 
    } 
}
輸出:
Size of Spliterator : 4
list of Numbers:
22
34
45
67

實物圖2:

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

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html#spliterator–



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ArrayBlockingQueue spliterator() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。