Composing 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
1234567891011121314151617package 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
12345678910111213141516171819202122package 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()?
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
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?
Awesome!
Completion rate improved to 5.56
Composing Lambdas
Swipe um das Menü anzuzeigen
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
1234567891011121314151617package 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
12345678910111213141516171819202122package 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()?
Danke für Ihr Feedback!