Creating Streams in Java
Before you can filter or transform data with streams, you need a stream to start with. Java offers many ways to create one: from a collection, an array, individual values, a range of numbers, a formula, the characters or lines of a string, or the lines of a file. In this lesson, you will learn each way to create a stream, with an example.
Ways to Create a Stream
| Source | Method | Type of stream |
|---|---|---|
| A collection | list.stream(), set.stream() | Stream<T> |
| A map | map.entrySet().stream(), map.keySet().stream() | Stream<...> |
| An array | Arrays.stream(array) | Stream<T> or IntStream... |
| Individual values | Stream.of(a, b, c) | Stream<T> |
| A possibly-null value | Stream.ofNullable(x) (Java 9) | 0 or 1 element |
| A range of numbers | IntStream.range(a, b), rangeClosed(a, b) | IntStream |
| A formula | Stream.iterate(seed, next) | Infinite unless limited |
| A formula with a stop condition | Stream.iterate(seed, hasNext, next) (Java 9) | Finite |
| A supplier | Stream.generate(supplier) | Infinite unless limited |
| Characters of a string | str.chars() | IntStream |
| Lines of a string | str.lines() (Java 11) | Stream<String> |
| Lines of a file | Files.lines(path) | Stream<String> |
| No elements | Stream.empty() | Empty stream |
| Built step by step | Stream.builder() | Stream<T> |
Example: From Collections, Arrays and Values
Let's see an example of the most common sources.
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Stream;
public class Sources {
public static void main(String[] args) {
List<String> list = List.of("Navika", "Srihitha");
System.out.println(list.stream().count());
String[] array = {"Advitha", "Anamaneni"};
System.out.println(Arrays.stream(array).toList());
int[] marks = {91, 78, 85};
System.out.println(Arrays.stream(marks).max().getAsInt());
System.out.println(Stream.of("a", "b", "c").toList());
Map<String, Integer> m =
new TreeMap<>(Map.of("Navika", 91, "Advitha", 85));
m.entrySet().stream()
.forEach(e -> System.out.println(e.getKey() + "=" + e.getValue()));
}
}2
[Advitha, Anamaneni]
91
[a, b, c]
Advitha=85
Navika=91Explanation: list.stream() and Arrays.stream(array) stream existing data. Arrays.stream on an int[] gives an IntStream, which has max(). Stream.of streams the values you list. A map isn't a collection, so you stream one of its views, such as entrySet().
Example: Ranges, iterate and generate
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Generated {
public static void main(String[] args) {
System.out.println(IntStream.range(1, 5).boxed().toList());
System.out.println(IntStream.rangeClosed(1, 5).sum());
System.out.println(Stream.iterate(1, n -> n * 2).limit(6).toList());
System.out.println(
Stream.iterate(1, n -> n < 100, n -> n * 3).toList());
System.out.println(Stream.generate(() -> "hi").limit(3).toList());
}
}[1, 2, 3, 4]
15
[1, 2, 4, 8, 16, 32]
[1, 3, 9, 27, 81]
[hi, hi, hi]Explanation: range(1, 5) excludes 5; rangeClosed(1, 5) includes it. Stream.iterate(1, n -> n * 2) is infinite, so limit(6) stops it. The three-argument iterate stops by itself when n < 100 becomes false. generate calls a supplier again and again, so it also needs limit.
Example: Strings and Builders
import java.util.stream.Stream;
public class TextSources {
public static void main(String[] args) {
long vowels = "Anamaneni".toLowerCase().chars()
.filter(c -> "aeiou".indexOf(c) >= 0)
.count();
System.out.println("Vowels: " + vowels);
String text = "Navika\nSrihitha\nAdvitha";
System.out.println(text.lines().map(String::length).toList());
Stream<String> built = Stream.<String>builder()
.add("one").add("two").build();
System.out.println(built.toList());
}
}Vowels: 5
[6, 8, 7]
[one, two]Explanation: chars() streams the characters of a string as int codes, which can be tested. lines() splits text into a stream of lines. A builder lets you add elements one at a time before building the stream.
Common Mistakes
Calling stream() on an array. Arrays have no methods; use Arrays.stream(arr).
Passing an int[] to Stream.of. You get a stream with one element, the array; use Arrays.stream or IntStream.of.
Forgetting to close Files.lines. Use it in try-with-resources, because it holds the file open.
Practice
- Easy: Print the multiples of 5 from 5 to 50 using
IntStream.rangeClosedandfilter. - Harder: Use
Stream.iterateto generate the first 10 Fibonacci numbers (hint: iterate over two-elementint[]arrays).
Key Takeaways
Was this tutorial helpful?