xxxxxxxxxx
// Java program to create Stream from Collections
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Function convert a List into Stream
private static <T> void getStream(T[] arr)
{
// Create stream from an array
// using Stream.of()
Stream<T> streamOfArray = Stream.of(arr);
// Iterate list first to last element
Iterator<T> it = streamOfArray.iterator();
// Iterate stream object
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args)
{
// Get the array
String[] arr
= new String[] { "a", "b", "c" };
// Get the Stream from the Array
getStream(arr);
}
}
xxxxxxxxxx
The stream represents a sequence of objects from a source such as a collection, which supports aggregate operations.
You can use Stream to filter, collect, print, and convert from one data structure to another, etc.
Stream does not store elements. It simply conveys elements from a source such as a data structure, an array, or an I/O channel, through a pipeline of computational operations.
Stream is functional in nature.
Operations performed on a stream do not modify its source.
For example, filtering a Stream obtained from a collection produces a new Stream without the filtered elements, rather than removing elements from the source collection.