-
Notifications
You must be signed in to change notification settings - Fork 1
Topic 05: Functional Programming
Zhamri Che Ani edited this page May 6, 2026
·
2 revisions
To introduce students to lambda expressions and functional programming concepts in Java, enabling them to write concise, modern, and efficient code using functional interfaces, predefined functional interfaces, and method references.
Students should be able to:
- Explain the concept of lambda expressions in Java.
- Understand the principles of functional programming.
- Identify and define functional interfaces.
- Use lambda expressions to implement functional interfaces.
- Apply lambda syntax with different parameter and return types.
- Compare traditional anonymous classes with lambda expressions.
- Use predefined functional interfaces from the java.util.function package.
- Apply:
PredicateFunctionConsumerSupplierBiPredicateBiFunctionBiConsumer
- Pass lambda expressions as method parameters.
- Explain variable capture in lambda expressions.
- Use method references for:
- Static methods
- Instance methods
- Constructors
- Develop Java applications using functional programming style.
- Java SE 8 features
- Anonymous methods
- Functional programming concepts
- Advantages of lambda expressions
- Single Abstract Method (SAM)
@FunctionalInterface- Common functional interfaces:
ComparatorRunnableActionListener
Example:
@FunctionalInterface
interface Checker {
boolean test(Person p);
}General syntax:
(parameters) -> { body }Examples:
() -> System.out.println("Hello")
s -> System.out.println(s)
(a, b) -> a + bRunnable r = new Runnable() {
public void run() {
System.out.println("Hello");
}
};Runnable r = () -> System.out.println("Hello");List<String> names = List.of("Ali", "Abu", "Siti");
names.forEach(name -> System.out.println(name));- Passing functions as method parameters
- Filtering collections
- Code reusability
Example:
printBy(people, p -> p.getAge() > 25);- Local variables
- Instance variables
- Static variables
- Effectively final variables
Predicate
Predicate<Integer> p = n -> n > 0;Function
Function<String, Integer> f = s -> s.length();Consumer
Consumer<String> c = s -> System.out.println(s);Supplier
Supplier<Integer> s = () -> new Random().nextInt(10);BiFunction
BiFunction<String, String, Integer> bf =
(a, b) -> a.length() + b.length();Instead of:
name -> System.out.println(name)Use:
System.out::printlnStatic Method Reference
Function<Integer, Double> squareRoot = Math::sqrt;Instance Method Reference
Consumer<String> display = myClass::print;Constructor Reference
BiFunction<Integer, String, Student> s = Student::new;| Feature | Lambda Expression | Method Reference |
|---|---|---|
| Syntax | More flexible | More concise |
| Readability | Medium | High |
| Use Case | Complex logic | Simple method calls |
- Filtering data (students, products)
- Processing collections
- Event handling
- Parallel data processing
Students create lambda expressions for:
- Printing messages
- Performing arithmetic operations
Example:
(a, b) -> a + bStudents develop a program that:
- Creates a
Personlist - Sorts the list by:
- Name
- Age
Using:
Comparator- Lambda expressions
Students create:
- Custom functional interface
Checker - Lambda expressions for filtering students
Example:
p -> p.getAge() > 20Students implement:
- Number validation using
Predicate - String length calculation using
Function
Students create programs that:
- Display messages using
Consumer - Generate random numbers using
Supplier
Students convert lambda expressions into:
- Static method references
- Object method references
- Constructor references
Activity 7: Student Management Mini System
Students develop a Java application that:
- Stores student data in
ArrayList - Uses lambda expressions to:
- Filter students
- Sort students
- Display selected students
Students create a program showing:
- Access to instance variables
- Access to static variables
- Restrictions on local variables in lambda expressions
- https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
- https://www.w3schools.com/java/java_lambda.asp
- https://www.tutorialspoint.com/java/java-lambda-expressions.htm
- https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
- https://www.baeldung.com/java-method-references
- https://www.tutorialspoint.com/java/java_method_references.htm