当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LinkedBlockingDeque spliterator()用法及代码示例


LinkedBlockingDeque的spliterator()方法在LinkedBlockingDeque的元素上返回分隔符。返回的迭代器是弱一致性的。 Spliterator可以与Java 8中的Streams一起使用。Spliterator也可以单独和批量遍历元素。

用法:

public Spliterator spliterator()

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


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

示例1:

// Java Program to demonstrate spliterator() 
// method of LinkedBlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create object of LinkedBlockingDeque 
        LinkedBlockingDeque<Integer> LBD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // Add elements 
        LBD.add(22); 
        LBD.add(34); 
        LBD.add(45); 
        LBD.add(67); 
  
        // create Spliterator of Deque 
        // using spliterator() method 
        Spliterator<Integer> numbers = LBD.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 to demonstrate spliterator() 
// method of LinkedBlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create object of LinkedBlockingDeque 
        LinkedBlockingDeque<String> LBD 
            = new LinkedBlockingDeque<String>(); 
  
        // Add numbers to front of LinkedBlockingDeque 
        LBD.add("Geeks"); 
        LBD.add("forGeeks"); 
        LBD.add("A"); 
        LBD.add("Computer"); 
        LBD.add("Portal"); 
  
        // create Spliterator of Deque 
        // using spliterator() method 
        Spliterator<String> numbers = LBD.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 : 5
list of Strings:
Geeks
forGeeks
A
Computer
Portal

参考: <https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingDeque.html#spliterator–



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 LinkedBlockingDeque spliterator() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。