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


Java ConcurrentSkipListSet spliterator()用法及代碼示例


java.util.concurrent.ConcurrentSkipListSet.spliterator()方法是Java中的內置函數,該函數在該集合的各個元素之間返回弱統一的Spliterator。

用法:

ConcurrentSkipListSet.spliterator()

參數:該函數不接受任何參數。


返回值:該函數在該集合的元素之間返回一個分隔符。

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

示例1:

// 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<String> set =  
                    new ConcurrentSkipListSet<String>(); 
  
        // Adding elements to this set 
        set.add("Gfg"); 
        set.add("is"); 
        set.add("best!!"); 
  
        // spliterator split and iterate 
        // the split parts in parallel 
        Spliterator<String> 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

示例2:

// 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<Character> set =  
                         new ConcurrentSkipListSet<Character>(); 
  
        // 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<Character> 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#spliterator–



相關用法


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