VOOZH about

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

⇱ Convert an Iterator to Stream in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert an Iterator to Stream in Java

Last Updated : 11 Dec, 2018
Given an Iterator, the task is to convert it into Stream 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:
  1. Get the Iterator.
  2. Convert the iterator to Spliterator using Spliterators.spliteratorUnknownSize() 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: