Lambda Expressions & Streams
On this page
- π§ What Are Lambda Expressions?
- β Why Use Lambdas?
- π§ Syntax
- π― Functional Interface
- π¦ Lambda Example with Functional Interface
Letβs jump into one of the most exciting and modern parts of Java β introduced in Java 8 β that really transforms how you write clean, expressive, and functional-style code:
β‘ Lambda Expressions & Streams in Java
π§ What Are Lambda Expressions?
Lambda expressions are a shorthand way to represent an anonymous function β a block of code that can be passed around and executed later.
β Before Java 8:
To pass behavior, you had to create an anonymous class:
Runnable r = new Runnable() {
public void run() {
System.out.println("Running...");
}
};
β With Lambda (Java 8+):
Runnable r = () -> System.out.println("Running...");
β Why Use Lambdas?
Benefit | Description |
---|---|
Concise Code | Less boilerplate than anonymous classes |
Functional Style | Makes code easier to read and reason about |
Used in Streams, Collections | Enables powerful operations like filter, map, reduce |
Foundation of functional interfaces | Used in APIs like Comparator , Runnable , Predicate |
π§ Syntax
(parameters) -> { body }
Examples:
// No parameters
() -> System.out.println("Hi")
// One parameter
name -> System.out.println("Hello " + name)
// Multiple parameters
(a, b) -> a + b
π― Functional Interface
A functional interface is an interface with only one abstract method.
It can have multiple default
or static
methods.
Examples from Java:
Runnable
Callable
Comparator<T>
Predicate<T>
Function<T, R>
Consumer<T>
You can use the @FunctionalInterface
annotation to enforce this.
@FunctionalInterface
interface MyFunction {
void apply();
}
π¦ Lambda Example with Functional Interface
@FunctionalInterface
interface Greeting {
void sayHello(String name);
}
public class LambdaDemo {
public static void main(String[] args) {
Greeting g = (name) -> System.out.println("Hello " + name);
g.sayHello("Alice");
}
}
π Java Streams API
π What Are Streams?
Streams are a new way to process data in a functional style.
You can perform complex data processing like filtering, mapping, sorting, and reducing with clean, readable code.
β Key Characteristics:
- Declarative (What to do, not how)
- Chainable
- Lazy (operations run only when needed)
- Donβt modify the original collection
π¦ Stream Pipeline
Collection -> Stream -> Intermediate Ops -> Terminal Op
π Common Stream Operations
πΉ Intermediate Operations
Operation | Description |
---|---|
filter | Keep elements matching a condition |
map | Transform each element |
sorted | Sort stream |
distinct | Remove duplicates |
limit | Take only N items |
πΉ Terminal Operations
Operation | Description |
---|---|
forEach | Iterate over elements |
collect | Convert to List, Set, Map |
count | Count elements |
reduce | Combine into single result |
π§ Stream Example
import java.util.*;
public class StreamExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Alex", "Brian");
names.stream()
.filter(name -> name.startsWith("A"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println); // Output: ALEX, ALICE
}
}
π§ Real-World Use-Cases
Task | Traditional Java | Stream API |
---|---|---|
Filter records | Loops and ifs | filter() |
Transform objects | Manual mapping | map() |
Aggregate values | Loop and manual total | reduce() or collect() |
Count, max, min | Manual comparison | count() , max() |
π₯ Stream + Collectors
List<String> names = Arrays.asList("Sam", "Samantha", "Sara");
List<String> result = names.stream()
.filter(name -> name.startsWith("S"))
.collect(Collectors.toList());
π§ Summary: Lambda vs Stream
Feature | Lambda | Stream |
---|---|---|
Concept | Anonymous function | Sequence of data operations |
Goal | Define behavior | Process data in a declarative way |
Common Usage | Functional Interfaces | Collections, Maps, Arrays |
π Homework for Students
1. Functional Interface & Lambda
- Create an interface
MathOperation
with a methodint operate(int a, int b)
- Implement
add
,subtract
,multiply
as lambda expressions
2. Streams Practice
Given a list of names:
List<String> names = Arrays.asList("John", "Jane", "Jake", "Jill", "Jack");
Use Stream API to:
- Filter names starting with
J
- Convert to uppercase
- Sort alphabetically
- Collect into a new
List<String>
3. Use reduce()
to calculate the sum of a list of integers.
4. Bonus: Create a method that returns the longest string from a list using Streams.
Would you like to continue into Method References and Optional, or dive into Collections Framework deep-dive, or go toward File I/O, Threads, or JDBC next?