🎓 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 ObjLongConsumer interface is a functional interface that represents an operation that accepts an object and a long-valued argument and returns no result. It is part of the java.util.function package and is commonly used for operations that involve both an object and a long value, such as modifying an object's state.
Table of Contents
- What is
ObjLongConsumer? - Methods and Syntax
- Examples of
ObjLongConsumer - Real-World Use Case
- Conclusion
1. What is ObjLongConsumer?
ObjLongConsumer is a functional interface that performs an operation on an object and a long value without returning any result. It is useful for scenarios where an object's state needs to be modified based on a long input.
2. Methods and Syntax
The main method in the ObjLongConsumer interface is:
void accept(T t, long value): Performs this operation on the given object andlongargument.
Syntax
ObjLongConsumer<T> objLongConsumer = (T t, long value) -> {
// operation on t and value
};
3. Examples of ObjLongConsumer
Example 1: Updating Account Balance
import java.util.function.ObjLongConsumer;
class Account {
private String name;
private long balance;
public Account(String name, long balance) {
this.name = name;
this.balance = balance;
}
public void deposit(long amount) {
this.balance += amount;
}
@Override
public String toString() {
return name + "'s balance: " + balance;
}
}
public class AccountUpdateExample {
public static void main(String[] args) {
Account account = new Account("John", 100L);
// Define an ObjLongConsumer that deposits a long value to an account
ObjLongConsumer<Account> deposit = (acc, amount) -> acc.deposit(amount);
deposit.accept(account, 50L);
System.out.println(account);
}
}
Output:
John's balance: 150
Example 2: Adding Quantity to a Product
import java.util.function.ObjLongConsumer;
class Product {
private String name;
private long quantity;
public Product(String name, long quantity) {
this.name = name;
this.quantity = quantity;
}
public void addQuantity(long amount) {
this.quantity += amount;
}
@Override
public String toString() {
return name + "'s quantity after addition: " + quantity;
}
}
public class ProductQuantityExample {
public static void main(String[] args) {
Product product = new Product("Laptop", 10L);
// Define an ObjLongConsumer that adds quantity to a product
ObjLongConsumer<Product> addQuantity = (prod, amount) -> prod.addQuantity(amount);
addQuantity.accept(product, 5L);
System.out.println(product);
}
}
Output:
Laptop's quantity after addition: 15
4. Real-World Use Case: Logging Timestamps
In applications, ObjLongConsumer can be used to log timestamps to a logger object.
import java.util.function.ObjLongConsumer;
class Logger {
private String id;
public Logger(String id) {
this.id = id;
}
public void logTimestamp(long timestamp) {
System.out.println("Logger " + id + " recorded timestamp: " + timestamp);
}
}
public class TimestampLoggingExample {
public static void main(String[] args) {
Logger logger = new Logger("Logger1");
// Define an ObjLongConsumer to log timestamps
ObjLongConsumer<Logger> logTimestamp = (log, time) -> log.logTimestamp(time);
logTimestamp.accept(logger, System.currentTimeMillis());
}
}
Output:
Logger Logger1 recorded timestamp: [current timestamp in milliseconds]
Conclusion
The ObjLongConsumer interface is a versatile tool in Java for performing operations on an object and a long value without returning a result. It simplifies handling tasks like updating object states or logging information. Using ObjLongConsumer can lead to cleaner and more efficient code, especially in functional programming contexts.
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