Skip to content

Topic 05: Functional Programming

Zhamri Che Ani edited this page May 6, 2026 · 2 revisions

Goal

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.

🎯 Learning Objectives

Students should be able to:

  1. Explain the concept of lambda expressions in Java.
  2. Understand the principles of functional programming.
  3. Identify and define functional interfaces.
  4. Use lambda expressions to implement functional interfaces.
  5. Apply lambda syntax with different parameter and return types.
  6. Compare traditional anonymous classes with lambda expressions.
  7. Use predefined functional interfaces from the java.util.function package.
  8. Apply:
    • Predicate
    • Function
    • Consumer
    • Supplier
    • BiPredicate
    • BiFunction
    • BiConsumer
  9. Pass lambda expressions as method parameters.
  10. Explain variable capture in lambda expressions.
  11. Use method references for:
    • Static methods
    • Instance methods
    • Constructors
  12. Develop Java applications using functional programming style.

📌 Topics to Cover

1. Introduction to Lambda Expressions

  • Java SE 8 features
  • Anonymous methods
  • Functional programming concepts
  • Advantages of lambda expressions

2. Functional Interfaces

  • Single Abstract Method (SAM)
  • @FunctionalInterface
  • Common functional interfaces:
    • Comparator
    • Runnable
    • ActionListener

Example:

@FunctionalInterface
interface Checker {
    boolean test(Person p);
}

3. Lambda Expression Syntax

General syntax:

(parameters) -> { body }

Examples:

() -> System.out.println("Hello")

s -> System.out.println(s)

(a, b) -> a + b

4. Traditional vs Lambda Approach

Without Lambda

Runnable r = new Runnable() {
    public void run() {
        System.out.println("Hello");
    }
};

With Lambda

Runnable r = () -> System.out.println("Hello");

5. Using Lambda with Collections

List<String> names = List.of("Ali", "Abu", "Siti");

names.forEach(name -> System.out.println(name));

6. Functional Programming with Lambda

  • Passing functions as method parameters
  • Filtering collections
  • Code reusability

Example:

printBy(people, p -> p.getAge() > 25);

7. Variable Capture in Lambda

  • Local variables
  • Instance variables
  • Static variables
  • Effectively final variables

8. Predefined Functional Interfaces

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();

9. Method References

Instead of:

name -> System.out.println(name)

Use:

System.out::println

Static 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;

Lambda vs Method Reference

Feature Lambda Expression Method Reference
Syntax More flexible More concise
Readability Medium High
Use Case Complex logic Simple method calls

Real-world Applications

  • Filtering data (students, products)
  • Processing collections
  • Event handling
  • Parallel data processing

🛠️ Hands-on Activity

Activity 1: Simple Lambda Expression

Students create lambda expressions for:

  • Printing messages
  • Performing arithmetic operations

Example:

(a, b) -> a + b

Activity 2: Comparator with Lambda

Students develop a program that:

  • Creates a Person list
  • Sorts the list by:
    • Name
    • Age

Using:

  • Comparator
  • Lambda expressions

Activity 3: Filtering Data with Functional Interface

Students create:

  • Custom functional interface Checker
  • Lambda expressions for filtering students

Example:

p -> p.getAge() > 20

Activity 4: Using Predicate and Function

Students implement:

  • Number validation using Predicate
  • String length calculation using Function

Activity 5: Consumer and Supplier

Students create programs that:

  • Display messages using Consumer
  • Generate random numbers using Supplier

Activity 6: Method References

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

Activity 8: Variable Capture Demonstration

Students create a program showing:

  • Access to instance variables
  • Access to static variables
  • Restrictions on local variables in lambda expressions

📚 Suggested Readings

  1. https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
  2. https://www.w3schools.com/java/java_lambda.asp
  3. https://www.tutorialspoint.com/java/java-lambda-expressions.htm
  4. https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
  5. https://www.baeldung.com/java-method-references
  6. https://www.tutorialspoint.com/java/java_method_references.htm

Clone this wiki locally