🎓 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 average() method in Java, part of the java.util.stream.LongStream interface, is used to calculate the average of the elements in the stream. This method is useful when you need to compute the mean value of a sequence of long values.
Table of Contents
- Introduction
average()Method Syntax- Understanding
average() - Examples
- Basic Usage
- Using
average()in Combination with Other Stream Operations
- Real-World Use Case
- Conclusion
Introduction
The average() method is a terminal operation that calculates the average of the elements in the stream. The method returns an OptionalDouble, which will be empty if the stream is empty.
average() Method Syntax
The syntax for the average() method is as follows:
OptionalDouble average()
Parameters:
- This method does not take any parameters.
Returns:
- An
OptionalDoubledescribing the average of the elements in the stream, or an emptyOptionalDoubleif the stream is empty.
Throws:
- This method does not throw any exceptions.
Understanding average()
The average() method computes the average of all elements in the stream. If the stream is empty, the method returns an empty OptionalDouble. Otherwise, it returns an OptionalDouble containing the average value.
Examples
Basic Usage
To demonstrate the basic usage of average(), we will create a LongStream and calculate the average of its elements.
Example
import java.util.OptionalDouble;
import java.util.stream.LongStream;
public class AverageExample {
public static void main(String[] args) {
LongStream stream = LongStream.of(1L, 2L, 3L, 4L, 5L);
// Use average() to calculate the average of the elements
OptionalDouble average = stream.average();
// Print the average if present
average.ifPresent(System.out::println);
}
}
Output:
3.0
Using average() in Combination with Other Stream Operations
This example shows how to use average() in combination with other stream operations, such as filtering.
Example
import java.util.OptionalDouble;
import java.util.stream.LongStream;
public class AverageWithFilterExample {
public static void main(String[] args) {
LongStream stream = LongStream.of(10L, 20L, 30L, 40L, 50L);
// Use average() to calculate the average of the filtered elements
OptionalDouble average = stream.filter(n -> n > 20).average();
// Print the average if present
average.ifPresent(System.out::println);
}
}
Output:
40.0
Real-World Use Case
Calculating Average Transaction Amount
In real-world applications, the average() method can be used to calculate the average amount of transactions from a stream of transaction values.
Example
import java.util.OptionalDouble;
import java.util.stream.LongStream;
public class AverageTransactionExample {
public static void main(String[] args) {
LongStream transactionAmounts = LongStream.of(1000L, 2000L, 1500L, 3000L, 2500L);
// Use average() to calculate the average transaction amount
OptionalDouble average = transactionAmounts.average();
// Print the average transaction amount if present
average.ifPresent(amount -> System.out.println("Average Transaction Amount: " + amount));
}
}
Output:
Average Transaction Amount: 2000.0
Conclusion
The LongStream.average() method is used to calculate the average of the elements in the stream. This method is particularly useful for computing the mean value of a sequence of long values. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring accurate calculations of averages.
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