Sub topic 4
Creating Streams from Collections, Arrays, and Values
Every stream pipeline starts with a source. A source is the place the elements come from. This chapter covers the three most common sources: collections, arrays, and values you type directly into your code.
Why the Source Matters
A stream does not store data. It reads elements from a source, one at a time. So before you can filter or collect anything, you must ask a source for a stream. Java gives you a simple way to do this for each kind of source.
The Three Main Ways
| Source | How to get a stream | Example |
|---|---|---|
| Collection (List, Set) | Call `stream()` on it | `list.stream()` |
| Array | Call `Arrays.stream(array)` | `Arrays.stream(names)` |
| Separate values | Call `Stream.of(...)` | `Stream.of("a", "b")` |
A collection is an object that holds a group of elements, such as a List or a Set. An array is a fixed-size row of elements, written like String[].
A Worked Example
This program builds a stream from each kind of source and prints the elements.
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
public class SourcesExample {
public static void main(String[] args) {
// 1. From a collection
List<String> fruits = List.of("apple", "banana", "cherry");
fruits.stream().forEach(System.out::println);
// 2. From an array
String[] colors = {"red", "green", "blue"};
Arrays.stream(colors).forEach(System.out::println);
// 3. From separate values
Stream.of("one", "two", "three").forEach(System.out::println);
// 4. From a map, through its entries
Map<String, Integer> ages = Map.of("Ann", 30);
ages.entrySet().stream()
.forEach(e -> System.out.println(e.getKey() + " is " + e.getValue()));
// 5. An empty stream
System.out.println(Stream.empty().count()); // 0
}
}The output is:
apple
banana
cherry
red
green
blue
one
two
three
Ann is 30
0Here, forEach runs an action on each element. You will learn more about it in a later chapter.
Streams from a Map
A Map stores key and value pairs. It has no stream() method of its own. Instead, you pick which part you want to stream:
map.keySet().stream()gives the keys.map.values().stream()gives the values.map.entrySet().stream()gives each key and value together.
Arrays of Numbers
Arrays.stream also works on number arrays such as int[]. For an int[] it returns an IntStream, a special stream for whole numbers. A later chapter explains these number streams.
Exercise
You have the array String[] pets = {"cat", "dog", "fish"};. Write one line of code that creates a stream from it and prints each pet.
Show answer
Arrays.stream(pets).forEach(System.out::println); You could also write Stream.of(pets).forEach(System.out::println);, because Stream.of accepts an array as its list of values. Both need the matching import: java.util.Arrays or java.util.stream.Stream.
Exercise
Why does myMap.stream() fail to compile, and what can you write instead to stream the map's values?
Show answer
Map is not a collection and has no stream() method. Use myMap.values().stream() to stream the values.