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


Java ConcurrentLinkedDeque Spliterator()用法及代碼示例


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

用法:

public Spliterator spliterator()

返回值:此方法在ConcurrentLinkedDeque中的元素上返回拆分器。


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

示例1:

// Java Program to demonstrate spliterator() 
// method of ConcurrentLinkedDeque 
  
import java.util.concurrent.ConcurrentLinkedDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create object of ConcurrentLinkedDeque 
        ConcurrentLinkedDeque<Integer> CBD 
            = new ConcurrentLinkedDeque<Integer>(); 
  
        // Add elements 
        CBD.add(22); 
        CBD.add(34); 
        CBD.add(45); 
        CBD.add(67); 
  
        // create Spliterator of Deque 
        // using spliterator() method 
        Spliterator<Integer> numbers 
            = CBD.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 : 9223372036854775807
list of Numbers:
22
34
45
67

示例2:

// Java Program to demonstrate spliterator() 
// method of ConcurrentLinkedDeque 
  
import java.util.concurrent.ConcurrentLinkedDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create object of ConcurrentLinkedDeque 
        ConcurrentLinkedDeque<String> CBD 
            = new ConcurrentLinkedDeque<String>(); 
  
        // Add numbers to front of ConcurrentLinkedDeque 
        CBD.add("Geeks"); 
        CBD.add("forGeeks"); 
        CBD.add("A"); 
        CBD.add("Computer"); 
        CBD.add("Portal"); 
  
        // create Spliterator of Deque 
        // using spliterator() method 
        Spliterator<String> numbers 
            = CBD.spliterator(); 
  
        // getExactSize of Spliterator 
        System.out.println("Size of Spliterator : "
                           + numbers.estimateSize()); 
  
        System.out.println("list of Strings:"); 
  
        // forEachRemaining method of Spliterator 
        numbers.forEachRemaining( 
            (n) -> System.out.println(n)); 
    } 
}
輸出:
Size of Spliterator : 9223372036854775807
list of Strings:
Geeks
forGeeks
A
Computer
Portal


相關用法


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