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


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


Java Vector 類的 spliterator() 方法用於在此列表中的元素上創建後期綁定和 fail-fast 拆分器。

用法

以下是 spliterator() 方法的聲明:

public Spliterator<E> spliterator()

參數

此方法不接受任何參數。

返回

spliterator() 方法在列表中的元素上返回一個拆分器。

異常

NA

兼容版本

Java 1.8 及以上

例子1

import java.util.*;
import java.util.stream.Stream;
public class VectorSpliteratorExample1 {  
	public static void main(String arg[]) {	
	  //Creating an empty vector object
	  Vector<Integer> v1 = new Vector<Integer>(); 		
	  //Adding elemnts to the vector
	  v1.add(10); 
	  v1.add(30);
	  v1.add(20);
	  v1.add(40);		
	  //Obtain a Stream to the vector
        Stream<Integer> str = v1.stream();
	  //Getting Spliterator object on vector v1
        Spliterator<Integer> splitr1 = str.spliterator();        
        //Printing estimateSize of vector
        System.out.println("Estimated size:" + splitr1.estimateSize());
        System.out.println("Contents of vector:");
        //Use forEachRemaining method    
        splitr1.forEachRemaining((n) -> System.out.println(n));
        }
}

輸出:

Estimated size:4
Contents of vector:
10
30
20
40

例子2

import java.util.*;
import java.util.stream.Stream;
public class VectorSpliteratorExample2 {  
	public static void main(String arg[]) {	
	  Vector<String> vec = new Vector<>();
        vec.addElement("One");
        vec.addElement("Two");
        vec.addElement("Three");
        vec.addElement("Four");
        vec.addElement("Five");	
	  //Obtain a Stream to the vector
        Stream<String> str = vec.stream();
	  //Getting Spliterator object on vector vec
        Spliterator<String> splitr1 = str.spliterator();        
        //Printing estimateSize of vector
        System.out.println("Estimated size:" + splitr1.estimateSize());
        System.out.println("Contents of vector:");
        //Use forEachRemaining method    
        splitr1.forEachRemaining((n) -> System.out.println(n));
        }
}

輸出:

Estimated size:5
Contents of vector:
One
Two
Three
Four
Five






相關用法


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