🎓 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
Introduction
In Java, the LongConsumer interface is a functional interface that represents an operation that accepts a single long-valued argument and returns no result. It is part of the java.util.function package and is commonly used for operations that process long values, such as logging or modifying data.
Table of Contents
- What is
LongConsumer? - Methods and Syntax
- Examples of
LongConsumer - Real-World Use Case
- Conclusion
1. What is LongConsumer?
LongConsumer is a functional interface that performs an operation on a single long input. It is often used in lambda expressions and method references for handling long values.
2. Methods and Syntax
The main methods in the LongConsumer interface are:
void accept(long value): Performs this operation on the givenlongargument.default LongConsumer andThen(LongConsumer after): Returns a composedLongConsumerthat performs this operation followed by theafteroperation.
Syntax
LongConsumer longConsumer = (long value) -> {
// operation on value
};
3. Examples of LongConsumer
Example 1: Printing a Long Value
import java.util.function.LongConsumer;
public class PrintLongExample {
public static void main(String[] args) {
// Define a LongConsumer that prints a long value
LongConsumer print = (value) -> System.out.println("Value: " + value);
print.accept(10L);
}
}
Output:
Value: 10
Example 2: Multiplying and Displaying
import java.util.function.LongConsumer;
public class MultiplyExample {
public static void main(String[] args) {
// Define a LongConsumer that multiplies a long by 2 and prints the result
LongConsumer multiplyAndPrint = (value) -> System.out.println("Multiplied Value: " + (value * 2));
multiplyAndPrint.accept(5L);
}
}
Output:
Multiplied Value: 10
Example 3: Using andThen
import java.util.function.LongConsumer;
public class AndThenExample {
public static void main(String[] args) {
// Define two LongConsumers
LongConsumer print = (value) -> System.out.println("Value: " + value);
LongConsumer multiply = (value) -> System.out.println("Multiplied Value: " + (value * 2));
// Compose the LongConsumers
LongConsumer combined = print.andThen(multiply);
combined.accept(7L);
}
}
Output:
Value: 7
Multiplied Value: 14
4. Real-World Use Case: Logging Timestamps
In applications, LongConsumer can be used to log timestamps represented by long values.
import java.util.function.LongConsumer;
public class TimestampLogger {
public static void main(String[] args) {
// Define a LongConsumer to log timestamps
LongConsumer logTimestamp = (timestamp) -> System.out.println("Logged timestamp: " + timestamp);
logTimestamp.accept(System.currentTimeMillis());
}
}
Output:
Logged timestamp: [current timestamp in milliseconds]
Conclusion
The LongConsumer interface is a versatile tool in Java for performing operations on long values without returning a result. It simplifies handling tasks like logging, modifying data, or processing real-time information. Using LongConsumer can lead to cleaner and more efficient code, especially in functional programming contexts. The andThen method allows for easy composition of operations, enhancing code modularity.
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