📌  相关文章
📜  Java中的 ConcurrentLinkedDeque Spliterator() 方法及示例

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

Java中的 ConcurrentLinkedDeque Spliterator() 方法及示例

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

句法:

public Spliterator spliterator()

返回:此方法在 ConcurrentLinkedDeque 中的元素上返回一个Spliterator

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

方案一:

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

方案二:

// 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 CBD
            = new ConcurrentLinkedDeque();
  
        // 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 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