🎓 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 sum() method in Java, part of the java.util.stream.DoubleStream interface, is used to calculate the sum of elements in a DoubleStream. This method is useful when you need to aggregate the elements of a stream to get their total sum.
Table of Contents
- Introduction
sum()Method Syntax- Understanding
sum() - Examples
- Basic Usage
- Using
sum()with Other Stream Operations
- Real-World Use Case
- Conclusion
Introduction
The sum() method is a terminal operation that returns the sum of elements in a DoubleStream. This method is particularly useful when you need to quickly calculate the total sum of a sequence of double values.
sum() Method Syntax
The syntax for the sum() method is as follows:
double sum()
Parameters:
- This method does not take any parameters.
Returns:
- The sum of elements in the stream.
Throws:
- This method does not throw any exceptions.
Understanding sum()
The sum() method allows you to quickly calculate the total sum of all elements in a DoubleStream. It is a terminal operation, meaning it consumes the stream and produces a single result.
Examples
Basic Usage
To demonstrate the basic usage of sum(), we will create a DoubleStream and use sum() to calculate the total sum of its elements.
Example
import java.util.stream.DoubleStream;
public class SumExample {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);
// Calculate the sum of the elements in the stream
double sum = doubleStream.sum();
// Print the sum
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 16.5
Using sum() with Other Stream Operations
This example shows how to use sum() in combination with other stream operations, such as filtering.
Example
import java.util.stream.DoubleStream;
public class SumWithOtherOperationsExample {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0);
// Filter the elements and calculate the sum of the remaining elements
double sum = doubleStream.filter(n -> n > 2.0).sum();
// Print the sum
System.out.println("Sum of elements greater than 2.0: " + sum);
}
}
Output:
Sum of elements greater than 2.0: 12.0
Real-World Use Case
Calculating Total Sales
In real-world applications, the sum() method can be used to calculate the total sales from a stream of sales amounts.
Example
import java.util.stream.DoubleStream;
public class TotalSalesExample {
public static void main(String[] args) {
DoubleStream salesAmounts = DoubleStream.of(250.75, 320.50, 120.00, 450.25, 600.00);
// Calculate the total sales
double totalSales = salesAmounts.sum();
// Print the total sales
System.out.println("Total Sales: " + totalSales);
}
}
Output:
Total Sales: 1741.5
Conclusion
The DoubleStream.sum() method is used to calculate the sum of elements in a stream. This method is particularly useful for scenarios where you need to aggregate the elements of a stream to get their total sum. By understanding and using this method, you can efficiently manage and process streams of double values 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