📌  相关文章
📜  Java中的LinkedBlockingDeque spliterator() 方法

📅  最后修改于: 2022-05-13 01:55:03.145000             🧑  作者: Mango

Java中的LinkedBlockingDeque spliterator() 方法

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

句法:

public Spliterator spliterator()

返回:此方法在LinkedBlockingDeque中的元素上返回 Spliterator。

下面的程序说明了 LinkedBlockingDeque 的 spliterator() 方法:

方案一:

// 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 LBD
            = new LinkedBlockingDeque();
  
        // Add elements
        LBD.add(22);
        LBD.add(34);
        LBD.add(45);
        LBD.add(67);
  
        // create Spliterator of Deque
        // using spliterator() method
        Spliterator 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

方案二:

// 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 LBD
            = new LinkedBlockingDeque();
  
        // 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 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

参考: