Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Composing Lambdas | Advanced Lambda Usage
Lambda Expressions in Java

bookComposing Lambdas

When you want to build more complex logic from simple, reusable pieces, function composition is a powerful technique. In Java, the Function interface provides two key default methods for composition: andThen and compose. Both allow you to combine two functions, but the order of execution differs. With andThen, the first function is applied, and its result is passed to the second function. With compose, the second function is applied first, and its result is passed to the first function. This enables you to create sophisticated transformations by chaining simple lambdas together.

Main.java

Main.java

copy
1234567891011121314151617
package com.example; import java.util.function.Function; public class Main { public static void main(String[] args) { Function<Integer, Integer> multiplyByTwo = x -> x * 2; Function<Integer, Integer> addTen = x -> x + 10; Function<Integer, Integer> multiplyThenAdd = multiplyByTwo.andThen(addTen); Function<Integer, Integer> addThenMultiply = multiplyByTwo.compose(addTen); System.out.println("multiplyThenAdd(5): " + multiplyThenAdd.apply(5)); // (5 * 2) + 10 = 20 System.out.println("addThenMultiply(5): " + addThenMultiply.apply(5)); // (5 + 10) * 2 = 30 } }

Function composition is especially useful in scenarios like data validation and transformation pipelines. For example, you might want to validate user input, sanitize it, and then map it to a domain object—all by chaining small, focused lambdas. By composing functions this way, you keep your code modular, readable, and easy to maintain. You can also reuse the same building blocks in different combinations, reducing duplication and increasing flexibility.

Main.java

Main.java

copy
12345678910111213141516171819202122
package com.example; import java.util.function.Predicate; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Predicate<String> isNotEmpty = s -> !s.isEmpty(); Predicate<String> startsWithA = s -> s.startsWith("A"); Predicate<String> isNotEmptyAndStartsWithA = isNotEmpty.and(startsWithA); List<String> names = List.of("Alice", "", "Bob", "Anna", "Andrew", ""); List<String> filtered = names.stream() .filter(isNotEmptyAndStartsWithA) .collect(Collectors.toList()); System.out.println(filtered); // Output: [Alice, Anna, Andrew] } }

1. Which method is used to chain two Function lambdas so the first function is applied and then the result is passed to the second function?

2. What is the result of composing two Predicate lambdas with and()?

question mark

Which method is used to chain two Function lambdas so the first function is applied and then the result is passed to the second function?

Select the correct answer

question mark

What is the result of composing two Predicate lambdas with and()?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you give an example of function composition in Java?

What is the difference between andThen and compose in practice?

How can I use function composition for data validation?

bookComposing Lambdas

Scorri per mostrare il menu

When you want to build more complex logic from simple, reusable pieces, function composition is a powerful technique. In Java, the Function interface provides two key default methods for composition: andThen and compose. Both allow you to combine two functions, but the order of execution differs. With andThen, the first function is applied, and its result is passed to the second function. With compose, the second function is applied first, and its result is passed to the first function. This enables you to create sophisticated transformations by chaining simple lambdas together.

Main.java

Main.java

copy
1234567891011121314151617
package com.example; import java.util.function.Function; public class Main { public static void main(String[] args) { Function<Integer, Integer> multiplyByTwo = x -> x * 2; Function<Integer, Integer> addTen = x -> x + 10; Function<Integer, Integer> multiplyThenAdd = multiplyByTwo.andThen(addTen); Function<Integer, Integer> addThenMultiply = multiplyByTwo.compose(addTen); System.out.println("multiplyThenAdd(5): " + multiplyThenAdd.apply(5)); // (5 * 2) + 10 = 20 System.out.println("addThenMultiply(5): " + addThenMultiply.apply(5)); // (5 + 10) * 2 = 30 } }

Function composition is especially useful in scenarios like data validation and transformation pipelines. For example, you might want to validate user input, sanitize it, and then map it to a domain object—all by chaining small, focused lambdas. By composing functions this way, you keep your code modular, readable, and easy to maintain. You can also reuse the same building blocks in different combinations, reducing duplication and increasing flexibility.

Main.java

Main.java

copy
12345678910111213141516171819202122
package com.example; import java.util.function.Predicate; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Predicate<String> isNotEmpty = s -> !s.isEmpty(); Predicate<String> startsWithA = s -> s.startsWith("A"); Predicate<String> isNotEmptyAndStartsWithA = isNotEmpty.and(startsWithA); List<String> names = List.of("Alice", "", "Bob", "Anna", "Andrew", ""); List<String> filtered = names.stream() .filter(isNotEmptyAndStartsWithA) .collect(Collectors.toList()); System.out.println(filtered); // Output: [Alice, Anna, Andrew] } }

1. Which method is used to chain two Function lambdas so the first function is applied and then the result is passed to the second function?

2. What is the result of composing two Predicate lambdas with and()?

question mark

Which method is used to chain two Function lambdas so the first function is applied and then the result is passed to the second function?

Select the correct answer

question mark

What is the result of composing two Predicate lambdas with and()?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3
some-alt