3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Create functional interfaces in Java.
- Pass objects to functions.
- Create objects using functions.
- Use Java Streams for data processing.
- Apply map operations to transform data.
3.2 Overview
Functional programming is a programming style that focuses on functions, immutability, and data transformation. Java supports functional programming through:
- lambda expressions,
- functional interfaces,
- streams,
- and built-in functional interfaces.
Functional programming is important in:
- enterprise software,
- data processing,
- automation,
- and scalable application development.
This lesson introduces learners to practical functional programming techniques in Java.
Understanding functional programming is important because it simplifies code and improves readability and maintainability.
PA0301 — Build Interfaces onto Object Classes
Functional interfaces contain a single abstract method.
Java Example:
@FunctionalInterface
interface Greeting {
void sayHello(String name);
}
class Student {
String name;
Student(String name) {
this.name = name;
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Greeting g = (n) -> System.out.println(“Welcome, “ + n + “!”);
Student s = new Student(“Alice”);
g.sayHello(s.name);
}
}
Practical Activity
Learners must:
- create functional interfaces,
- apply lambda expressions,
- and connect interfaces to object classes.
PA0302 — Pass an Object to a Function
Objects can be passed to functions using functional interfaces.
Java Example:
import java.util.function.Consumer;
class Student {
String name;
int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
}
public class PassObject {
public static void main(String[] args) {
Student s1 = new Student(“John”, 85);
Consumer display = (s) ->
System.out.println(s.name + “ scored “ + s.marks);
display.accept(s1);
}
}
Practical Activity
Learners must:
- pass objects to functions,
- apply Consumer interfaces,
- and display object information.
PA0303 — Create an Object Using a Function
Functional interfaces can create objects dynamically.
Java Example:
import java.util.function.Supplier;
class Car {
String model;
Car(String model) {
this.model = model;
}
}
public class CreateObject {
public static void main(String[] args) {
Supplier carSupplier = () -> new Car(“Toyota Corolla”);
Car c1 = carSupplier.get();
System.out.println(“Created a car: “ + c1.model);
}
}
Practical Activity
Learners must:
- create objects using functions,
- apply Supplier interfaces,
- and display object information.
PA0304 — Squares of First 10 Numbers
Java Streams simplify mathematical operations.
Java Example:
import java.util.stream.IntStream;
public class Squares {
public static void main(String[] args) {
IntStream.rangeClosed(1, 10)
.map(n -> n * n)
.forEach(System.out::println);
}
}
Practical Activity
Learners must:
- use streams,
- generate mathematical outputs,
- and process numeric data.
PA0305 — Use map Operation
The map() function transforms data inside streams.
Java Example:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MapExample {
public static void main(String[] args) {
List names = Arrays.asList(“john”, “mary”, “paul”);
List upperNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(upperNames);
}
}
Practical Activity
Learners must:
- apply map operations,
- transform collection data,
- and display updated values.
AK0301 — Functions and Principles of Functional Programming in Java
Functional Programming Feature — Function
Lambda Expressions — Simplify function creation
Streams — Process collections efficiently
map() — Transform data
Consumer — Accept objects without return values
Supplier — Create and supply objects
Practical Activity
Learners must:
- identify functional programming features,
- explain stream processing,
- and apply functional programming concepts.
AK0302 — Deeper Understanding of Thought Processes
Functional programming encourages:
- cleaner logic,
- immutability,
- less repetitive code,
- and structured data transformation.
Practical Activity
Learners must:
- explain functional programming benefits,
- compare programming approaches,
- and apply logical thinking processes.
IAC0301 — Expected Results with Functional Programming in Java are Achieved
Expected Results
- Functional interfaces are implemented correctly.
- Lambda expressions function correctly.
- Objects are passed successfully to functions.
- Streams process data correctly.
- map() operations transform values successfully.
3.5 Key Notes / Summary
- Functional programming focuses on functions and data transformation.
- Lambda expressions simplify Java code.
- Functional interfaces contain one abstract method.
- Streams process collections efficiently.
- map() transforms collection data.
- Consumer accepts objects.
- Supplier creates objects dynamically.