Java Lambdas & Streams

Functional-style programming.

✍️ 2 exercises in this lesson

A lambda is a short anonymous function: (a, b) -> a + b.

The Stream API processes collections in a pipeline:

  • filter(): keep matching items
  • map(): transform items
  • sorted(), limit(), distinct()
  • collect(Collectors.toList()) or toList(), sum(), count(), average()

Key Takeaways: Java Lambdas & Streams

Mastering Java Lambdas & Streams helps you write clean and reliable Java code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Java Lambdas & Streams Code Example

Java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> marks = List.of(72, 95, 58, 88, 64);

        List<Integer> passed = marks.stream()
            .filter(m -> m >= 60)
            .sorted(Comparator.reverseOrder())
            .toList();

        double avg = marks.stream().mapToInt(Integer::intValue).average().orElse(0);

        System.out.println(passed);
        System.out.println("Average: " + avg);

        List.of("ali", "sara").forEach(n -> System.out.println(n.toUpperCase()));
    }
}
Output
[95, 88, 72, 64]
Average: 75.4
ALI
SARA
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

Java Lambdas & Streams Practice Exercises

Check what you learned in “Java Lambdas & Streams”.

/ 2 solved
Fill in the blank

Keep only marks of 60 or more.

marks.stream().(m -> m >= 60)
✓ Correct! Not quite. Try again.
Multiple choice

What is "(a, b) -> a + b"?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services