Friday, September 5, 2025
HomeLanguagesJavaJava Program to Convert Iterator to Spliterator

Java Program to Convert Iterator to Spliterator

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:

  1. Get the Iterator.
  2. Convert the iterator to Spliterator using Spliterators.spliteratorUnknownSize() method.
  3. Return the Spliterator.

Below is the implementation of the above approach:




// 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);
    }
}


Output:

1
2
3
4
5
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS