🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the Consumer andThen() method in Java programming and how to use it with an example.
1. Consumer andThen() Method Overview
Definition:
The Consumer.andThen() method provides a way to compose two instances of Consumer, effectively chaining them together. After performing its operation, the first Consumer will pass its input to the second Consumer.
Syntax:
default Consumer<T> andThen(Consumer<? super T> after)
Parameters:
- after: The next Consumer operation to be executed after the current operation.
Key Points:
- The andThen() method allows for the sequential combination of multiple Consumer operations.
- If the andThen() operation's argument throws an exception, it is relayed to the caller.
- If the original Consumer (the one calling andThen()) throws an exception, the after Consumer will not be executed.
2. Consumer andThen() Method Example
import java.util.function.Consumer;
public class ConsumerAndThenExample {
public static void main(String[] args) {
Consumer<String> toUpperCase = s -> System.out.println(s.toUpperCase());
Consumer<String> appendExclamation = s -> System.out.println(s + "!");
// Chaining the two consumers
Consumer<String> chainedConsumer = toUpperCase.andThen(appendExclamation);
String input = "hello";
chainedConsumer.accept(input);
}
}
Output:
HELLO hello!
Explanation:
In the provided example, we have two Consumer instances. The first, toUpperCase, converts a string to uppercase and prints it.
The second, appendExclamation, appends an exclamation mark to the string and prints it.
Using andThen(), we chain these two consumers together into chainedConsumer.
When we call accept() on chainedConsumer, the input string "hello" is first transformed to "HELLO" by toUpperCase and then printed with an appended exclamation mark by appendExclamation.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment