Java Interface Spliterator 的 estimateSize() 方法用于估计剩余元素的数量,如果无限、未知或计算成本太高,则返回 Long.MAX_VALUE。
用法
以下是 estimateSize() 方法的声明-
long estimateSize()
参数
此方法不接受任何参数。
返回
estimateSize() 方法返回元素的估计大小或 Long.MAX_VALUE 如果无限、未知或计算成本太高。
异常
NA
兼容版本
Java 1.8 及以上
例子1
import java.util.*;
public class InterfaceSpliteratorEstimateSizeExample1 {
public static void main(String args[]){
//Create an array list
ArrayList<Integer> al1 = new ArrayList<>();
//Add values in the list.
al1.add(1);
al1.add(-2);
al1.add(3);
al1.add(-4);
//Getting Spliterator object
Spliterator<Integer> splitr = al1.spliterator();
//Print EstimateSize
System.out.println("Estimate size of ArrayList:" + splitr.estimateSize());
//Printelements of arraylist.
System.out.println("Elements of arraylist:");
while(splitr.tryAdvance((n) -> System.out.println(n)));
}
}
输出:
Estimate size of ArrayList:4 Elements of arraylist: 1 -2 3 -4
例子2
import java.util.*;
public class InterfaceSpliteratorEstimateSizeExample2 {
public static void main(String args[]){
List<String> nameList = Arrays.asList("Rahul", "Abhi", "Suresh", "Rani", "Nikki", "Rosy", "Prem");
Spliterator<String> splitr = nameList.spliterator();
//Print EstimateSize
System.out.println("Estimate size of List:" + splitr.estimateSize());
}
}
输出:
Estimate size of List:7
例子3
import java.util.*;
public class InterfaceSpliteratorEstimateSizeExample3 {
public static void main(String args[]){
List<String> names = new ArrayList<>();
names.add("Rahul");
names.add("Rohit");
names.add("Surya");
//Getting Spliterator object
Spliterator<String> namesSpliterator = names.spliterator();
System.out.println("Estimate size:" + namesSpliterator.estimateSize());
System.out.println("Name present in List:");
namesSpliterator.forEachRemaining(System.out::println);
System.out.println("Again Estimate size is:" + namesSpliterator.estimateSize());
}
}
输出:
Estimate size:3 Name present in List: Rahul Rohit Surya Again Estimate size is:0
相关用法
- Java Spliterator hasCharacteristics()用法及代码示例
- Java Spliterator forEachRemaining()用法及代码示例
- Java Spliterator getExactSizeIfKnown()用法及代码示例
- Java Spliterator trySplit()用法及代码示例
- Java Spliterator tryAdvance()用法及代码示例
- Java Spliterator characteristics()用法及代码示例
- Java Spliterator getComparator()用法及代码示例
- Java Guava Splitter limit()用法及代码示例
- Java Guava Splitter omitEmptyStrings()用法及代码示例
- Java Guava Splitter splitToList()用法及代码示例
- Java Guava Splitter trimResults()用法及代码示例
- Java Guava Splitter fixedLength()用法及代码示例
- Java SimpleDateFormat equals()用法及代码示例
- Java StringReader read()用法及代码示例
- Java Stack addAll(Collection)用法及代码示例
- Java StringBuilder toString()用法及代码示例
- Java SimpleDateFormat parse()用法及代码示例
- Java String valueOf()用法及代码示例
- Java SimpleTimeZone setRawOffset()用法及代码示例
- Java StrictMath log()用法及代码示例
注:本文由纯净天空筛选整理自 Java Spliterator estimateSize() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。