VOOZH about

URL: https://www.geeksforgeeks.org/java/convert-an-iterable-to-stream-in-java/

⇱ Convert an Iterable to Stream in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert an Iterable to Stream in Java

Last Updated : 11 Dec, 2018
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:
  1. Get the Iterable.
  2. Convert the Iterable to Spliterator using Iterable.spliterator() method.
  3. Convert the formed Spliterator into Sequential Stream using StreamSupport.stream() method.
  4. Return the stream.
Below is the implementation of the above approach:
Output:
1
2
3
4
5
Comment
Article Tags:
Article Tags: