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


Java Spliterator getExactSizeIfKnown()用法及代码示例


Java Interface Spliterator的getExactSizeIfKnown()方法用于获取SIZED拆分器的estimateSize(),否则返回-1。

用法

以下是 getExactSizeIfKnown() 方法的声明-

default long getExactSizeIfKnown()

参数

此方法不接受任何参数。

返回

getExactSizeIfKnown() 方法返回分离器的 estimateSize()(如果已知),否则返回 -1。

异常

NA

兼容版本

Java 1.8 及以上

例子1

import java.util.*;
public class InterfaceSpliteratorGetExactSizeIfKnownExample1 {  
     public static void main(String args[]){
    	   //Create an array list
         ArrayList<Integer> list = new ArrayList<>();             
         //Add values in the list.
         list.add(1001);
         list.add(-2001);
         list.add(3001);
         list.add(-4001);                                                     
         //Getting Spliterator object
         Spliterator<Integer> splitr = list.spliterator(); 
         //Print Estimate Size 
         System.out.println("Estimate size:" + splitr.estimateSize());
         //Print getExactSizeIfKnown 
         System.out.println("Exact size:" + splitr.getExactSizeIfKnown());
         //Printelements of arraylist.              
         System.out.println("Elements of arraylist-- ");     
         while(splitr.tryAdvance((n) -> System.out.println(n)));
         }
}

输出:

Estimate size:4
Exact size:4
Elements of arraylist-- 
1001
-2001
3001
-4001

例子2

import java.util.*;
public class InterfaceSpliteratorGetExactSizeIfKnownExample2 {  
    public static void main(String args[]){
   	    List<String> nameList = Arrays.asList("Rahul", "Nikki", "Prem");
   	    Spliterator<String> splitr = nameList.spliterator();
   	    //splitr.tryAdvance(System.out::println);
   	    System.out.println("Exact size before getting List elements:" +splitr.getExactSizeIfKnown());
   	    splitr.forEachRemaining(System.out::println);
   	    //Print getExactSizeIfKnown 
          System.out.println("Exact size after getting List elements:" + splitr.getExactSizeIfKnown());   	
          }
}

输出:

Exact size before getting List elements:3
Rahul
Nikki
Prem
Exact size after getting List elements:0



相关用法


注:本文由纯净天空筛选整理自 Java Spliterator getExactSizeIfKnown() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。