🎓 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 ObjDoubleConsumer interface is a functional interface that represents an operation that accepts an object and a double-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 double value, such as modifying an object's state.
Table of Contents
- What is
ObjDoubleConsumer? - Methods and Syntax
- Examples of
ObjDoubleConsumer - Real-World Use Case
- Conclusion
1. What is ObjDoubleConsumer?
ObjDoubleConsumer is a functional interface that performs an operation on an object and a double value without returning any result. It is useful for scenarios where an object's state needs to be modified based on a double input.
2. Methods and Syntax
The main method in the ObjDoubleConsumer interface is:
void accept(T t, double value): Performs this operation on the given object anddoubleargument.
Syntax
ObjDoubleConsumer<T> objDoubleConsumer = (T t, double value) -> {
// operation on t and value
};
3. Examples of ObjDoubleConsumer
Example 1: Updating Account Balance
import java.util.function.ObjDoubleConsumer;
class Account {
private String name;
private double balance;
public Account(String name, double balance) {
this.name = name;
this.balance = balance;
}
public void deposit(double 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", 100.0);
// Define an ObjDoubleConsumer that deposits a double value to an account
ObjDoubleConsumer<Account> deposit = (acc, amount) -> acc.deposit(amount);
deposit.accept(account, 50.0);
System.out.println(account);
}
}
Output:
John's balance: 150.0
Example 2: Applying a Discount to a Product
import java.util.function.ObjDoubleConsumer;
class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public void applyDiscount(double discount) {
this.price -= this.price * discount / 100;
}
@Override
public String toString() {
return name + "'s price after discount: " + price;
}
}
public class ProductDiscountExample {
public static void main(String[] args) {
Product product = new Product("Laptop", 1000.0);
// Define an ObjDoubleConsumer that applies a discount to a product
ObjDoubleConsumer<Product> applyDiscount = (prod, discount) -> prod.applyDiscount(discount);
applyDiscount.accept(product, 10.0);
System.out.println(product);
}
}
Output:
Laptop's price after discount: 900.0
4. Real-World Use Case: Logging Temperature Readings
In applications, ObjDoubleConsumer can be used to log temperature readings to a sensor object.
import java.util.function.ObjDoubleConsumer;
class Sensor {
private String id;
public Sensor(String id) {
this.id = id;
}
public void logTemperature(double temperature) {
System.out.println("Sensor " + id + " recorded temperature: " + temperature);
}
}
public class TemperatureLoggingExample {
public static void main(String[] args) {
Sensor sensor = new Sensor("A1");
// Define an ObjDoubleConsumer to log temperature readings
ObjDoubleConsumer<Sensor> logTemperature = (sens, temp) -> sens.logTemperature(temp);
logTemperature.accept(sensor, 25.5);
}
}
Output:
Sensor A1 recorded temperature: 25.5
Conclusion
The ObjDoubleConsumer interface is a versatile tool in Java for performing operations on an object and a double value without returning a result. It simplifies handling tasks like updating object states or logging information. Using ObjDoubleConsumer 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