🎓 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.LongStream interface, is used to calculate the sum of the elements in the stream. This method is useful when you need to quickly find the total of all the long values in the stream.
Table of Contents
- Introduction
sum()Method Syntax- Understanding
sum() - Examples
- Basic Usage
- Using
sum()with Filtered Streams
- Real-World Use Case
- Conclusion
Introduction
The sum() method is a terminal operation that returns the sum of the elements in the stream. It is particularly useful for aggregating long values, such as summing transaction amounts, calculating total scores, or other numerical data.
sum() Method Syntax
The syntax for the sum() method is as follows:
long sum()
Parameters:
- This method does not take any parameters.
Returns:
- The sum of the elements in the stream as a
long.
Throws:
- This method does not throw any exceptions.
Understanding sum()
The sum() method processes the elements of the stream and calculates their sum. Since it is a terminal operation, it consumes the stream and returns the total sum of its elements.
Examples
Basic Usage
To demonstrate the basic usage of sum(), we will create a LongStream and use sum() to calculate the total of its elements.
Example
import java.util.stream.LongStream;
public class SumExample {
public static void main(String[] args) {
LongStream stream = LongStream.of(1L, 2L, 3L, 4L, 5L);
// Use sum() to calculate the total sum of the elements
long total = stream.sum();
// Print the total sum
System.out.println("Total sum: " + total);
}
}
Output:
Total sum: 15
Using sum() with Filtered Streams
This example shows how to use sum() in combination with filtering to calculate the sum of elements that match a specific condition.
Example
import java.util.stream.LongStream;
public class SumWithFilterExample {
public static void main(String[] args) {
LongStream stream = LongStream.of(10L, 20L, 30L, 40L, 50L);
// Use sum() to calculate the sum of elements greater than 25
long total = stream.filter(n -> n > 25).sum();
// Print the total sum
System.out.println("Total sum of elements greater than 25: " + total);
}
}
Output:
Total sum of elements greater than 25: 120
Real-World Use Case
Calculating Total Revenue
In real-world applications, the sum() method can be used to calculate the total revenue from a stream of transaction amounts.
Example
import java.util.stream.LongStream;
public class SumTransactionsExample {
public static void main(String[] args) {
LongStream transactionAmounts = LongStream.of(1000L, 2000L, 1500L, 3000L, 2500L);
// Use sum() to calculate the total revenue
long totalRevenue = transactionAmounts.sum();
// Print the total revenue
System.out.println("Total Revenue: " + totalRevenue);
}
}
Output:
Total Revenue: 10000
Conclusion
The LongStream.sum() method is used to calculate the sum of the elements in the stream. This method is particularly useful for quickly finding the total of long values in a stream. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that elements are summed as needed.
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