Given an Iterable, the task is to convert it into Stream in Java.
Examples:
Input: Iterable = [1, 2, 3, 4, 5]
Output: {1, 2, 3, 4, 5}
Input: Iterable = ['G', 'e', 'e', 'k', 's']
Output: {'G', 'e', 'e', 'k', 's'}
Approach:
- Get the Iterable.
- Convert the Iterable to Spliterator using Iterable.spliterator() method.
- Convert the formed Spliterator into Sequential Stream using StreamSupport.stream() method.
- Return the stream.
Below is the implementation of the above approach: