🎓 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.DoubleStream interface, is used to calculate the average of the elements in a stream. This method is useful when you need to compute the average value of a sequence of double values.
Table of Contents
- Introduction
average()Method Syntax- Understanding
average() - Examples
- Basic Usage
- Handling an Empty Stream
- Real-World Use Case
- Conclusion
Introduction
The average() method returns an OptionalDouble describing the arithmetic mean of the elements of the stream, or an empty OptionalDouble if the stream is empty. This is a terminal operation.
average() Method Syntax
The syntax for the average() method is as follows:
OptionalDouble average()
Parameters:
- This method does not take any parameters.
Returns:
- An
OptionalDoublecontaining the average value of the elements of 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 value of all elements in a DoubleStream. If the stream is empty, it returns an empty OptionalDouble, which can be checked to determine if a value is present.
Examples
Basic Usage
To demonstrate the basic usage of average(), we will create a DoubleStream and calculate the average of its elements.
Example
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class AverageExample {
public static void main(String[] args) {
DoubleStream numbers = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);
// Calculate the average of the numbers
OptionalDouble average = numbers.average();
average.ifPresent(avg -> System.out.println("Average: " + avg));
}
}
Output:
Average: 3.3
Handling an Empty Stream
This example shows how to handle an empty stream when using the average() method.
Example
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class EmptyStreamExample {
public static void main(String[] args) {
DoubleStream emptyStream = DoubleStream.empty();
// Calculate the average of the empty stream
OptionalDouble average = emptyStream.average();
if (average.isPresent()) {
System.out.println("Average: " + average.getAsDouble());
} else {
System.out.println("The stream is empty, no average value.");
}
}
}
Output:
The stream is empty, no average value.
Real-World Use Case
Calculating Average Temperature
In real-world applications, the average() method can be used to calculate the average temperature from a list of temperature readings.
Example
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class TemperatureAverageExample {
public static void main(String[] args) {
DoubleStream temperatures = DoubleStream.of(36.5, 37.0, 36.8, 37.1, 36.9);
// Calculate the average temperature
OptionalDouble averageTemperature = temperatures.average();
averageTemperature.ifPresent(avg -> System.out.println("Average Temperature: " + avg));
}
}
Output:
Average Temperature: 36.86
Conclusion
The DoubleStream.average() method is used to calculate the average of the elements in a stream. This method is particularly useful for computing the average value of a sequence of double values. By understanding and using this method, you can efficiently perform average calculations on 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