Loading...

Java Streams

Start a pipeline from a list with stream(), keep or change elements, then print. In Pipe.java, keep the public class Pipe, import java.util.ArrayList, store "oak" and "pine", and call names.stream().filter(n -> n.startsWith("o")).forEach(n -> System.out.println(n));. Compile with javac Pipe.java and run java Pipe. The screen should show oak. A second demo uses map so the screen shows OAK, then PINE. Leave optional Records for the next page. This lesson is a stream pipeline, not a plain forEach on the list and not a Generics get(0) demo.

Filter one matching word

Open Pipe.java. Keep the public class name Pipe. Delete leftover Cue lambda-only demos, leftover Bin generics prints, leftover Flag arguments runs, and leftover Box Scanner prompts if they bury the streams story. The program must not wait for typing and must not require launch words.
Add the import at the top:
import java.util.ArrayList;
Inside Pipe.main, build a typed list with two words, then start a stream that keeps words beginning with o:
ArrayList<String> names = new ArrayList<>();
names.add("oak");
names.add("pine");
names.stream().filter(n -> n.startsWith("o")).forEach(n -> System.out.println(n));
stream() opens the pipeline. filter keeps elements whose lambda returns true. forEach prints each kept element. "oak" starts with o. "pine" does not. The screen shows oak.

Pipe stream filter prints oak

java
import java.util.ArrayList;

public class Pipe {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("oak");
        names.add("pine");
        names.stream().filter(n -> n.startsWith("o")).forEach(n -> System.out.println(n));
    }
}

From the folder that holds Pipe.java, run javac Pipe.java. When braces and semicolons match, javac writes no line of its own. Then run java Pipe — not java Pipe.java. Keep one public class so the name matches Pipe.
Walk the run once. Java starts at Pipe.main. The list holds "oak" and "pine". stream() begins. filter keeps "oak". forEach prints oak. Nothing prints both words. Nothing calls only names.forEach without stream(). Nothing reads args[0].
Say the beginner rule while you watch the screen: stream() starts the pipe; filter chooses; forEach finishes with a side effect print. Do not invent Records demos on this page. Records waits for the next optional lesson. Do not drop stream() and expect filter to hang on the list itself in this teaching shape.
If you still see oak then pine from a leftover Cue forEach file, switch to Pipe and keep the filter step. If javac rejects stream, confirm the list type is ArrayList and the call is names.stream().

Map every word to uppercase

Keep the same public class Pipe. Change the pipeline to transform each word instead of filtering:
names.stream().map(n -> n.toUpperCase()).forEach(n -> System.out.println(n));
Keep both "oak" and "pine" in the list. Compile again with javac Pipe.java and run java Pipe. The screen shows OAK, then PINE. map returns a new value for each element. forEach prints those values in order.

Pipe stream map prints OAK then PINE

java
import java.util.ArrayList;

public class Pipe {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("oak");
        names.add("pine");
        names.stream().map(n -> n.toUpperCase()).forEach(n -> System.out.println(n));
    }
}

Walk the run once. First mapped value is OAK. Second mapped value is PINE. Keep the two demos as two teaching shapes. First prove filter keeps one word. Then prove map changes every word. Both shapes share import, typed list, stream(), and a terminal forEach.
If you chain filter and map in your own experiments later, order matters. This page keeps one intermediate step per demo so the screen stays easy to read.

Pipeline habits

Keep the steps in order while you practice.

📊 Stream habits for Pipe

Step Call Beginner check
Start names.stream() opens the pipeline from the list
Keep some filter(n -> ...) lambda returns true to keep
Change each map(n -> ...) lambda returns the new value
Finish print forEach(n -> System.out.println(n)) terminal step that prints
Skip stream() list has no filter in this shape put stream() before filter or map

Streams is not Lambda-only and not Records

Lambda taught forEach directly on the list. Streams puts stream() first so you can filter or map before the final forEach. Records will introduce a compact data type on a later optional page. Do not mix those three jobs in one beginner file today.
oak kept by filter is still a list element flowing through a pipeline. It is not args[0] from Flag. When a leftover Cue file prints every element with no filter, leave that as history and keep Pipe's stream().filter line.
Without stream(), this page's filter and map calls are not the teaching shape. Without a terminal forEach, the pipeline does not print. Keep both ends of the pipe together while you practice each demo. Keep Records syntax off this page until that lesson, even if you have already skimmed a headline about compact data types elsewhere in the optional path.

Folder checks before Records

List the folder after a successful compile. You should see Pipe.java and Pipe.class together. Edit the source, save, run javac Pipe.java, then run java Pipe. A stale .class can show an old filter screen if you skip compile after switching to map.
If javac complains about duplicate public classes, keep only Pipe public. If the screen shows both oak and pine on the filter demo, the startsWith("o") check may be missing — restore the filter lambda. If nothing prints, confirm add ran before stream().
Do not paste Cue's list-only forEach as a replacement for the whole pipeline on this page. Do not paste Bin get(0) into Pipe. The optional streams job is stream plus one intermediate step plus forEach.

Before optional Records

You can keep public class Pipe, import ArrayList, add "oak" and "pine", call names.stream().filter(n -> n.startsWith("o")).forEach(...), see oak, then call names.stream().map(n -> n.toUpperCase()).forEach(...), see OAK then PINE. Compile with javac Pipe.java and run java Pipe. The table separates start, filter, map, and finish. The next optional page is Records.
Keep the Pipe demos for a clear streams pair. Keep Cue lambda files off this page when you practice pipelines. Keep Bin generics files off this page when you practice pipelines. Keep Records work off this page until that lesson. Those habits are the checkpoint before you move on. When the filter screen shows oak or the map screen shows OAK then PINE, Streams did its job for this lesson and you are ready for Records if you continue the optional path.
One last pass through the folder: one public Pipe, import present, typed ArrayList<String>, stream() before filter or map, terminal forEach, no Scanner wait, no launch words required. Run javac Pipe.java and java Pipe once more on the demo you keep open. If the screen still shows oak or OAK then PINE, the Streams shape is solid and the course can move to optional Records on the next page. Prefer one clear pipeline habit per read so each pass teaches the same map.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top