An instance of stream can be created from different sources. In this tutorial, we’ll discuss different ways to create a stream instance.
First, we’ll discuss stream of four different sources: array, a collection, generator function and I/O channel.
Stream of Array
There are two common methods used to create stream of Array: Stream.of() and Arrays.stream().
//String array
String[] strArray = new String[] {"one","two","three"};
//Stream of array using Stream.of()
Stream<String> streamOfArray = Stream.of(strArray);
//Stream of array using Arrays.stream()
Stream<String> streamOfArr = Arrays.stream(strArray);
import java.util.Arrays;
import java.util.Iterator;
import java.util.stream.Stream;
public class StreamExample {
public static void main(String args[]) {
//initialize string array
String[] strArray = new String[] {"one","two","three"};
//Stream of array using Stream.of()
Stream<String> streamOfArray = Stream.of(strArray);
//Stream of array using Arrays.stream()
Stream<String> streamOfArr = Arrays.stream(strArray);
Iterator<String> itr1 = streamOfArray.iterator();
Iterator<String> itr2 = streamOfArr.iterator();
// Iterate first stream object
while (itr1.hasNext()) {
System.out.print(itr1.next() + " ");
}
//to print empty line
System.out.println();
// Iterate second stream object
while (itr2.hasNext()) {
System.out.print(itr2.next() + " ");
}
}
}
Output
one two three
one two three
Stream of Collection
The source of stream can also be a collection. List.stream() is an example of creating stream of list.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class StreamExample {
public static void main(String args[]) {
//create a list of string
List<String> list = new ArrayList();
list.add("one");
list.add("two");
list.add("three");
Iterator<String> itr = list.iterator();
// Iterate first stream object
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
}
}
Output
one two three
Create a stream using Stream.generate()
generate() method accepts a Supplier and returns an infinite sequential unordered stream. This is suitable for generating constant streams, streams of random elements, etc. generate() method returns an infinite stream, so to restrict it we can provide a limit.
import java.util.stream.Stream;
public class StreamExample {
private static <T> void getStream(int limit) {
//create stream using generate() method
Stream.generate(Math::random)
.limit(limit)
.forEach(System.out::println);
}
public static void main(String args[]) {
// call getStream method to get stream using generate()
//method with limit 5
getStream(5);
}
}
Output
0.36224928907365705
0.6460205728645343
0.9987652362519464
0.3599350422313422
0.3277652917995362
Stream of File
Files class from java.nio has method lines() which returns lines from file as Stream.
String fileName = "d://file.txt";
Stream<String> stream = Files.lines(Paths.get(fileName))
Contents of file.txt
first line
second line
third line
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class StreamExample {
public static void main(String args[]) {
String fileName = "d://file.txt";
//read file into stream
// used try-with-resources as we need to close stream
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
first line
second line
third line
Now we’ll also see few other examples to generate stream. There are methods in Stream which return a Stream. In this tutorial we’ll discuss few of these cases.
Empty Stream
Stream.empty() method returns an empty sequential stream.
Stream<String> emptyStream = Stream.empty();
Create a stream from specified values using Stream.of()
Stream.of(T… values) returns a sequential ordered stream whose elements are the specified values.
import java.util.stream.Stream;
class StreamExample {
public static void main(String[] args) {
//Create a stream from specified string values
Stream<String> stream = Stream.of("Sun","Mon","Tue","Wed");
//Print sequential ordered stream
stream.forEach(element -> System.out.print(element + " "));
}
}
Output
Sun Mon Tue Wed
Create a stream using Stream.builder()
builder() method returns a builder for a Stream.
import java.util.stream.Stream;
class StreamExample {
public static void main(String[] args) {
//Create a builder of Stream
Stream.Builder<String> builder = Stream.builder();
//add elements
builder.add("one");
builder.add("two");
builder.add("three");
//build stream
Stream<String> stream = builder.build();
//print stream
stream.forEach(element -> System.out.print(element + " "));
}
}
Output
one two three
Create stream from Iterator
We need to use Spliterators.spliteratorUnknownSize to convert the Iterator into a Spliterator, followed by StreamSupport.stream to convert the Spliterator into a Stream.
import java.util.Arrays;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
class StreamExample {
public static void main(String[] args) {
Iterator<String> sourceIterator = Arrays.asList("A", "B", "C").iterator();
Stream<String> targetStream = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(sourceIterator, Spliterator.ORDERED),
false);
//print stream
targetStream.forEach(element -> System.out.print(element + " "));
}
}
Output
A B C
Create Stream from Iterable
First we have to get Iterable from Iterator. And then create Spliterator from Iterable, followed by StreamSupport.stream to convert Spliterator into a Stream.
import java.util.Arrays;
import java.util.Iterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
class StreamExample {
public static void main(String[] args) {
//create Iterator
Iterator<String> sourceIterator = Arrays.asList("A", "B", "C").iterator();
//create Iterable from Iterator
Iterable<String> iterable = () -> sourceIterator;
//Create Spliterator from Iterable, followed by creating stream from Spliterator
Stream<String> targetStream = StreamSupport.stream(iterable.spliterator(), false);
//print stream
targetStream.forEach(element -> System.out.print(element + " "));
}
}
Output
A B C
