Common Stream Functions
Use the common stream functions forEach, filter, map, distinct and reduce to process a list without writing loops.
Journeyman · Java · Updated
The Java Stream API provides many functions. This lesson covers five of them, and the next two lessons, Sorting, Cutting and Checking Streams and Counting, Collecting and Flattening Streams, cover the ones you'll use most after these.
Below are a few common Stream functions and usage examples.
forEach
The forEach function acts like a for loop and allows you to process each data item in a stream. The following forEach stream function prints each name to the console.
List<String> names = new ArrayList<>();
names.add("Olivia");
names.add("Ethan");
names.add("Sophia");
names.add("Liam");
names.add("Emma");
names.add("Noah");
names.add("Ava");
names.add("Jackson");
names.add("Mia");
names.add("Lucas");
names.stream()
.forEach(name -> {
System.out.println(name);
});
filter
The filter function acts like a SQL where clause, and filters out the data items that we don’t need.
List<String> names = new ArrayList<>();
names.add("Olivia");
names.add("Ethan");
names.add("Sophia");
names.add("Liam");
names.add("Emma");
names.add("Noah");
names.add("Ava");
names.add("Jackson");
names.add("Mia");
names.add("Lucas");
var filteredNames = names.stream()
.filter(name -> name.startsWith("E"))
.toList();
// filteredNames contains ["Ethan", "Emma"]
map
The map function allows us to change the shape of the data. For example we could turn a list of names (Strings) into a list of integers that represent the length of each name:
List<String> names = new ArrayList<>();
names.add("Olivia");
names.add("Ethan");
names.add("Sophia");
names.add("Liam");
names.add("Emma");
names.add("Noah");
names.add("Ava");
names.add("Jackson");
names.add("Mia");
names.add("Lucas");
var nameLengths = names.stream()
.map(name -> name.length())
.toList();
// nameLengths contains [6, 5, 6, 4, 4, 4, 3, 7, 3, 5]
distinct
The distinct function allows us to remove duplicate values, for example, if we have a list of names with multiple entries that are the same name, we can use distinct to remove duplicates.
List<String> names = new ArrayList<>();
names.add("Ethan");
names.add("Sophia");
names.add("Liam");
names.add("Emma");
names.add("Liam");
names.add("Mia");
names.add("Sophia");
var distinctNames = names.stream()
.distinct()
.toList();
// distinctNames does not have any duplicate names
reduce
The reduce function allows us to reduce a list of items into a single item. For example, we can quickly sum all numbers into a single value.
Notice that we do not add a toList() call at the end: reduce is a terminal operation, so it ends the stream and returns a single value.
0is the initial value the calculation starts fromcurrentSumis the temporary sum as all numbers are being added.numberis the next number in the stream [21, 79, 8, 15]
List<Integer> numbers = new ArrayList<>(Arrays.asList(21, 79, 8, 15));
int sum = numbers.stream()
.reduce(0, (currentSum, number) -> currentSum + number);
// sum is 123
Do it: print the name tags for one team
The coding club splits its members into teams by the first letter of their names. Before the first meeting, each team gets name tags printed in capital letters. The sign-up sheet has a problem: a few people signed up twice, and nobody needs two name tags.
The method gets two values:
signUps: the names on the sign-up sheet, aList<String>, in the order people signed up. A name can be on it more than once.letter: the team's letter, aString, such as"E".
For example, nameTags(signUps, "E") with this sign-up sheet:
List<String> signUps = List.of("Ethan", "Sophia", "Emma", "Liam", "Ethan", "Eli");
| Step | The names |
|---|---|
| The sign-up sheet | Ethan, Sophia, Emma, Liam, Ethan, Eli |
Only names that start with E |
Ethan, Emma, Ethan, Eli |
| Each name once | Ethan, Emma, Eli |
| In capital letters | ETHAN, EMMA, ELI |
So it returns ["ETHAN", "EMMA", "ELI"].
Your job: write the method nameTags(List<String> signUps, String letter) so it returns a List<String>: the team's name tags.
- Only the names that start with
letter. - Each name once, even when it's on the sheet twice.
- In the order they first signed up.
- Each name in capital letters.
- When no name starts with
letter, return an empty list.
Practiced live on the lesson page: write the Java right there and check it.
Learn more
Many additional stream functions exist. The next two lessons teach the ones Java developers reach for most after these five. Sorting, Cutting and Checking Streams covers sorting with a comparator, limit and skip, and anyMatch and findFirst. Counting, Collecting and Flattening Streams covers count, sum and average, Collectors.joining and groupingBy, and flatMap.
Sources
- Oracle, Stream (Java SE 25 & JDK 25): the API reference for
forEach,filter,map,distinct,reduceandtoList.