Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java iterator to stream

// Java program to get a Stream
// from a given Iterator
  
import java.util.*;
import java.util.stream.*;
  
class GFG {
  
    // Function to get the Stream
    public static <T> Stream<T>
    getStreamFromIterator(Iterator<T> iterator)
    {
  
        // Convert the iterator to Spliterator
        Spliterator<T>
            spliterator = Spliterators
                              .spliteratorUnknownSize(iterator, 0);
  
        // Get a Sequential Stream from spliterator
        return StreamSupport.stream(spliterator, false);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Get the Iterator
        Iterator<Integer>
            iterator = Arrays.asList(1, 2, 3, 4, 5)
                           .iterator();
  
        // Get the Stream from the Iterator
        Stream<Integer>
            stream = getStreamFromIterator(iterator);
  
        // Print the elements of stream
        stream.forEach(s -> System.out.println(s));
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: string to int 
Java :: java set foreach 
Java :: sorting in java 
Java :: param query spring boot 
Java :: car class java 
Java :: java scan a file 
Java :: how to convert primitive int to Integer in java 
Java :: bean factory vs application context 
Java :: statusbar text color android 
Java :: java swing dialog box 
Java :: encapsulation in oop 
Java :: java long to hours minutes and seconds 
Java :: Java Writer Using FileWriter 
Java :: is palindrome java 
Java :: how to set boolean to false if null java 
Java :: java stream loop 
Java :: mapping over a list of promises in javaascript 
Java :: set solid color background android programatically in drawable 
Java :: BufferredReader Class 
Java :: java instanceof keyword 
Java :: esponente in java 
Java :: how to make a dictionary in java 
Java :: java iterate map and remove 
Java :: declare java class 
Java :: H2 enabling remote database creation first 
Java :: indexof an array in java 
Java :: Powermockito static method call 
Java :: stringbuilder insert beginning 
Java :: work with arrays java 
Java :: android access main toolbar in fragment 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =