Last Updated : 23 Oct, 2018
Given an Iterator, the task is to convert it into Spliterators in Java.
Examples:Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'}Approach:
Below is the implementation of the above approach:
Java
// Java program to get a Spliterator
// from a given Iterator
import java.util.*;
class GFG {
// Function to get the Spliterator
public static <T> Spliterator<T>
getSpliteratorFromIterator(Iterator<T> iterator)
{
return Spliterators
.spliteratorUnknownSize(iterator, 0);
}
// Driver code
public static void main(String[] args)
{
// Get the Iterator
Iterator<Integer>
iterator = Arrays.asList(1, 2, 3, 4, 5)
.iterator();
// Get the Spliterator from the Iterator
Spliterator<Integer>
si = getSpliteratorFromIterator(iterator);
// Print the elements of Spliterator
si.forEachRemaining(System.out::println);
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4