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

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

Java中的 ConcurrentSkipListSet spliterator() 方法

Java .util.concurrent.ConcurrentSkipListSet.spliterator()方法是Java中的一个内置函数,它在该集合的元素之间返回一个弱统一的 Spliterator。

句法:

ConcurrentSkipListSet.spliterator()

参数:该函数不接受任何参数。

返回值:该函数在此集合的元素之间返回一个 Spliterator。

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

方案一:

// Java Program Demonstrate Spliterator()
// method of ConcurrentSkipListSet
  
import java.util.Spliterator;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetSpliteratorExample1 {
    public static void main(String[] args)
    {
        // Initializing the set
        ConcurrentSkipListSet set = 
                    new ConcurrentSkipListSet();
  
        // Adding elements to this set
        set.add("Gfg");
        set.add("is");
        set.add("best!!");
  
        // spliterator split and iterate
        // the split parts in parallel
        Spliterator str = set.spliterator();
  
        // performs the action for each remaining element
        str.forEachRemaining(
            (n) -> {
                String lc = n.toUpperCase();
                System.out.println(" Lower case = " + n);
                System.out.println(" Upper case = " + lc);
                System.out.println();
            });
    }
}
输出:
Lower case = Gfg
Upper case = GFG

Lower case = best!!
Upper case = BEST!!

Lower case = is
Upper case = IS

方案二:

// Java Program Demonstrate Spliterator()
// method of ConcurrentSkipListSet
  
import java.util.Spliterator;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetSpliteratorExample2 {
    public static void main(String[] args)
    {
        // Initializing the set
        ConcurrentSkipListSet set = 
                         new ConcurrentSkipListSet();
  
        // Adding elements to this set
        for (char ch = 'A'; ch <= 'Z'; ch++) {
            set.add(ch);
        }
  
        // Printing elements in the set
        System.out.print("The elements in the set are : ");
  
        // spliterator  split and iterate
        // the split parts in parallel
        Spliterator str = set.spliterator();
  
        // if element exists tryAdvance() will perform action
        while (str.tryAdvance((n) -> System.out.print(n + " ")))
            ;
    }
}
输出:
The elements in the set are : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#spliterator–