🎓 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
The summingDouble() method in Java, part of the java.util.stream.Collectors class, is used to calculate the sum of a double-valued function applied to the elements of a stream. This method is useful when you need to compute the sum of certain properties of the elements in a stream.
Table of Contents
- Introduction
summingDouble()Method Syntax- Understanding
summingDouble() - Examples
- Basic Usage
- Using
summingDouble()with Custom Objects
- Real-World Use Case
- Conclusion
Introduction
The summingDouble() method returns a Collector that produces the sum of a double-valued function applied to the input elements. This method is particularly useful for aggregating numeric data from a stream.
summingDouble() Method Syntax
The syntax for the summingDouble() method is as follows:
public static <T> Collector<T, ?, Double> summingDouble(ToDoubleFunction<? super T> mapper)
Parameters:
mapper: A function that extracts a double-valued property from an element.
Returns:
- A
Collectorthat produces the sum of the extracted double values.
Throws:
- This method does not throw any exceptions.
Understanding summingDouble()
The summingDouble() method allows you to sum the double values derived from the elements of a stream. This is useful in scenarios where you need to aggregate numeric data from a collection of objects.
Examples
Basic Usage
To demonstrate the basic usage of summingDouble(), we will create a list of double values and calculate their sum.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SummingDoubleExample {
public static void main(String[] args) {
List<Double> numbers = Arrays.asList(1.0, 2.5, 3.8, 4.2, 5.0);
// Calculate the sum of the numbers
Double sum = numbers.stream()
.collect(Collectors.summingDouble(Double::doubleValue));
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 16.5
Using summingDouble() with Custom Objects
This example shows how to use summingDouble() with a stream of custom objects to calculate the sum of a specific property.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SummingDoubleCustomObjectExample {
static class Product {
String name;
double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
double getPrice() {
return price;
}
}
public static void main(String[] args) {
List<Product> products = Arrays.asList(
new Product("Product A", 10.0),
new Product("Product B", 20.5),
new Product("Product C", 15.8),
new Product("Product D", 30.2),
new Product("Product E", 25.0)
);
// Calculate the sum of the prices of the products
Double totalPrice = products.stream()
.collect(Collectors.summingDouble(Product::getPrice));
System.out.println("Total Price: " + totalPrice);
}
}
Output:
Total Price: 101.5
Real-World Use Case
Calculating Total Sales Revenue
In real-world applications, the summingDouble() method can be used to calculate the total sales revenue from a list of sales transactions.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class TotalRevenueExample {
static class Transaction {
double amount;
Transaction(double amount) {
this.amount = amount;
}
double getAmount() {
return amount;
}
}
public static void main(String[] args) {
List<Transaction> transactions = Arrays.asList(
new Transaction(100.0),
new Transaction(200.0),
new Transaction(150.0),
new Transaction(50.0)
);
// Calculate the total revenue using summingDouble()
Double totalRevenue = transactions.stream()
.collect(Collectors.summingDouble(Transaction::getAmount));
System.out.println("Total Revenue: " + totalRevenue);
}
}
Output:
Total Revenue: 500.0
Conclusion
The Collectors.summingDouble() method is used to calculate the sum of a double-valued function applied to the elements of a stream. This method is particularly useful for aggregating numeric data from a collection of objects. By understanding and using this method, you can efficiently manage summing operations in your Java applications.
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