Java HashSet spliterator() Method
Example
Use a spliterator to loop through a set:
import java.util.HashSet;
import java.util.Spliterator;
public class Main {
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Spliterator<String> it = cars.spliterator();
it.forEachRemaining((n) -> System.out.println(n));
}
}
Definition and Usage
The spliterator() method returns a Spliterator object, which can be used to loop through the set.
A spliterator is like an iterator(), but it can also be split into two parts with its trySplit() method, so that different parts of the set can be handled at the same time.
The example uses a lambda expression with forEachRemaining() to print each item.
Syntax
public Spliterator<T> spliterator()
T refers to the data type of items in the set.
Parameter Values
None.
Technical Details
| Returns: | A Spliterator object for the set. |
|---|