tryAdvance() 是 Java Interface Spliterator 的一种方法,用于在对其执行指定操作时获取现有元素。
用法
以下是 tryAdvance() 方法的声明-
boolean tryAdvance(Consumer<? super T> action)
参数
参数 | 描述 | 必需/可选 |
---|---|---|
action | 它是检查元素的动作。 | Required |
返回
如果在进入此方法时存在下一个元素,则 tryAdvance() 方法返回 true,否则返回 false。
异常
NullPointerException - 如果指定的操作为空,则此方法抛出异常。
兼容版本
Java 1.8 及以上
例子1
import java.util.*;
public class InterfaceSpliteratorTryAdvanceExample1 {
public static void main(String args[]){
List<String> list = Arrays.asList("Jasmine", "Rose", "Lotus");
Spliterator<String> splitr = list.spliterator();
//Use tryAdvance() to display(action) elements of list.
boolean b1 = splitr.tryAdvance(System.out::println);
System.out.println("Element exists:"+b1);
System.out.println(" --- bulk traversal");
splitr.forEachRemaining(System.out::println);
System.out.println(" --Attempting tryAdvance again--");
boolean b2 = splitr.tryAdvance(System.out::println);
System.out.println("Element exists:"+b2);
}
}
输出:
Jasmine Element exists:true --- bulk traversal Rose Lotus --Attempting tryAdvance again-- Element exists:false
例子2
import java.util.*;
public class InterfaceSpliteratorTryAdvanceExample2 {
public static void main(String args[]){
List<String> nameList = Arrays.asList("Rahul", "Abhi", "Suresh", "Rani", "Nikki", "Rosy", "Prem");
Spliterator<String> splitr = nameList.spliterator();
while(splitr.tryAdvance((n) -> System.out.println("Name:" + n)));
}
}
输出:
Name:Rahul Name:Abhi Name:Suresh Name:Rani Name:Nikki Name:Rosy Name:Prem
例子3
import java.util.*;
public class InterfaceSpliteratorTryAdvanceExample3 {
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);
al1.add(5);
//Getting Spliterator object
Spliterator<Integer> splitr = al1.spliterator();
//Use tryAdvance() to display(action) elements of arraylist.
System.out.println("Elements of arraylist:");
while(splitr.tryAdvance((n) -> System.out.println(n)));
}
}
输出:
Elements of arraylist: 1 2 3 4 5
相关用法
- Java Spliterator trySplit()用法及代码示例
- Java Spliterator hasCharacteristics()用法及代码示例
- Java Spliterator estimateSize()用法及代码示例
- Java Spliterator forEachRemaining()用法及代码示例
- Java Spliterator getExactSizeIfKnown()用法及代码示例
- 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 tryAdvance() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。