📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ 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.IntStream
interface, is used to calculate the average of elements in an IntStream
. This method is useful when you need to find the mean value of a sequence of integers.
Table of Contents
- Introduction
average()
Method Syntax- Understanding
average()
- Examples
- Basic Usage
- Using
average()
with Other Stream Operations
- Real-World Use Case
- Conclusion
Introduction
The average()
method is a terminal operation that returns an OptionalDouble
describing the arithmetic mean of elements in an IntStream
. If the stream is empty, it returns an empty OptionalDouble
.
average() Method Syntax
The syntax for the average()
method is as follows:
OptionalDouble average()
Parameters:
- This method does not take any parameters.
Returns:
- An
OptionalDouble
describing the average of elements in the stream, or an emptyOptionalDouble
if the stream is empty.
Throws:
- This method does not throw any exceptions.
Understanding average()
The average()
method calculates the arithmetic mean of the elements in an IntStream
. The result is wrapped in an OptionalDouble
to handle the case where the stream might be empty. If the stream contains elements, the OptionalDouble
will contain the average value; otherwise, it will be empty.
Examples
Basic Usage
To demonstrate the basic usage of average()
, we will create an IntStream
and use average()
to calculate the average of its elements.
Example
import java.util.OptionalDouble; import java.util.stream.IntStream; public class AverageExample { public static void main(String[] args) { IntStream intStream = IntStream.of(1, 2, 3, 4, 5); // Calculate the average of the elements in the stream OptionalDouble average = intStream.average(); // Print the average if present average.ifPresent(System.out::println); } }
Output:
3.0
Using average()
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.IntStream; public class AverageWithOtherOperationsExample { public static void main(String[] args) { IntStream intStream = IntStream.of(1, 2, 3, 4, 5); // Filter the elements and calculate the average of the remaining elements OptionalDouble average = intStream.filter(n -> n > 2).average(); // Print the average if present average.ifPresent(System.out::println); } }
Output:
4.0
Real-World Use Case
Calculating Average Score
In real-world applications, the average()
method can be used to calculate the average score from a stream of student scores.
Example
import java.util.OptionalDouble; import java.util.stream.IntStream; public class AverageScoreExample { public static void main(String[] args) { IntStream scores = IntStream.of(75, 85, 90, 88, 92); // Calculate the average score OptionalDouble averageScore = scores.average(); // Print the average score if present averageScore.ifPresent(avg -> System.out.println("Average score: " + avg)); } }
Output:
Average score: 86.0
Conclusion
The IntStream.average()
method is used to calculate the average of elements in a stream. This method is particularly useful for finding the mean value of a sequence of integers. By understanding and using this method, you can efficiently calculate averages in your Java applications.
Comments
Post a Comment
Leave Comment