Java Spliterator tryAdvance() Method

The tryAdvance() is a method of Java Interface Spliterator which is used to get the existing elements upon performing the specified action on it.

Syntax

Following is the declaration of tryAdvance() method-

Parameter

ParameterDescriptionRequired/Optional
actionIt is the action for checking elements.Required

Return

The tryAdvance() method returns true if there is a next element present upon entry to this method, otherwise returns false.

Exceptions

NullPointerException- This method has thrown an exception if the specified action is null.

Compatibility Version

Java 1.8 and above

Example 1

Output:

Jasmine
Element exists: true
 --- bulk traversal
Rose
Lotus
 --Attempting tryAdvance again--
Element exists: false

Example 2

Output:

Name: Rahul
Name: Abhi
Name: Suresh
Name: Rani
Name: Nikki
Name: Rosy
Name: Prem

Example 3

Output:

Elements of arraylist: 
1
2
3
4
5