当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。