Loading...

Java Lambda

Pass a short action to forEach with an arrow. In Cue.java, keep the public class Cue, import java.util.ArrayList, store "oak" in a typed list, and call names.forEach(n -> System.out.println(n));. Compile with javac Cue.java and run java Cue. The screen should show oak. A second demo adds "pine" so the screen shows oak, then pine. Leave optional Streams for the next page. This lesson is a lambda passed to forEach, not a Generics get(0) demo and not command-line args.

One element, one lambda print

Open Cue.java. Keep the public class name Cue. Delete leftover Bin generics demos, leftover Flag program-arguments runs, leftover Box Scanner prompts, and leftover Shelf history if they bury the lambda 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 Cue.main, build a typed list, add one word, and pass a lambda to forEach:
ArrayList<String> names = new ArrayList<>();
names.add("oak");
names.forEach(n -> System.out.println(n));
The part n -> System.out.println(n) is the lambda. n is the parameter name for one element. The arrow -> points to the body that runs for that element. forEach calls that body once for "oak". The screen shows oak.

Cue forEach lambda prints oak

java
import java.util.ArrayList;

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

From the folder that holds Cue.java, run javac Cue.java. When braces and semicolons match, javac writes no line of its own. Then run java Cue — not java Cue.java. Keep one public class so the name matches Cue.
Walk the run once. Java starts at Cue.main. The list holds "oak". forEach takes the lambda. For the first (and only) element, n is "oak" and println writes oak. Nothing calls get(0) on this teaching shape. Nothing prints 3 from an Integer Bin demo. Nothing reads args[0].
Say the beginner rule while you watch the screen: forEach walks the list; the lambda is the action per element; the arrow separates the parameter from the body. Do not invent Streams pipelines on this page. Streams waits for the next optional lesson. Do not replace the lambda with a full named method on this teaching file — keep the arrow form.
If you still see only oak from names.get(0) in a leftover Bin file, switch to Cue and keep the forEach line. If javac rejects the arrow, check that you typed -> and that the lambda sits inside forEach(...).

Two elements, the same lambda

Keep the same public class Cue. Add a second word before forEach:
names.add("oak");
names.add("pine");
names.forEach(n -> System.out.println(n));
Compile again with javac Cue.java and run java Cue. The screen shows oak, then pine. forEach runs the lambda once per element in order. The lambda body does not change. Only the list length changed.

Cue forEach lambda prints oak then pine

java
import java.util.ArrayList;

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

Walk the run once. First call: n is "oak", print oak. Second call: n is "pine", print pine. Keep the two demos as two teaching shapes. First prove one lambda print. Then prove the same lambda on two elements. Both shapes share import, typed list, and n -> System.out.println(n).
If you print the whole list with println(names) instead of forEach, you see brackets and commas — that is a different habit. This page keeps one print per element through the lambda.

Lambda is not Generics-only and not Streams

Generics named the element type in ArrayList<String>. Lambda gives forEach a short action without writing a separate named method on this page. Streams will chain more list steps on a later optional page. Do not mix those three jobs in one beginner file today.
oak here is a list element visited by forEach. It is not args[0] from Flag and not a Scanner answer. When a leftover Bin file still prints only get(0), leave that as history and keep Cue's forEach line. When a leftover Flag file still needs java Flag oak, leave that launch habit aside and run plain java Cue.
Without the arrow, javac cannot treat the action as a lambda. Without forEach, the lambda has nowhere to run in this teaching shape. Keep both pieces together while you practice. Keep Streams syntax such as .stream().map off this page until that lesson, even if you have seen the word Stream in a headline somewhere else.

Folder checks before Streams

List the folder after a successful compile. You should see Cue.java and Cue.class together. Edit the source, save, run javac Cue.java, then run java Cue. A stale .class can show an old one-line screen if you skip compile after adding "pine".
If javac complains about duplicate public classes, keep only Cue public. If the screen shows [oak, pine] on one line, you printed the list object — switch back to forEach with println inside the lambda. If nothing prints, confirm add ran before forEach.
Do not paste Bin get(0) as a replacement for the lambda on this page. Do not paste Flag launch words into the run command. The optional lambda job is forEach with an arrow body.

Before optional Streams

You can keep public class Cue, import ArrayList, add "oak", call names.forEach(n -> System.out.println(n));, see oak, then add "pine" and see oak then pine. Compile with javac Cue.java and run java Cue. The next optional page is Streams.
Keep the Cue demos for a clear lambda pair. Keep Bin generics files off this page when you practice forEach. Keep Flag arguments files off this page when you practice forEach. Keep Streams work off this page until that lesson. Those habits are the checkpoint before you move on. When the one-word screen shows oak or the two-word screen shows oak then pine, Lambda did its job for this lesson and you are ready for Streams if you continue the optional path.
One last pass through the folder: one public Cue, import present, typed ArrayList<String>, forEach with n -> System.out.println(n), no Scanner wait, no launch words required. Run javac Cue.java and java Cue once more on the demo you keep open. If the screen still shows oak or oak then pine, the Lambda shape is solid and the course can move to optional Streams on the next page. Prefer one clear forEach 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