In Java Stream API, there are several collectors you can use to calculate the average of numbers. The purpose of averaging methods or collectors is to aggregate or combine the elements of the stream into a single summary result, which is the average of the elements.
A key feature of the Stream API is its ability to execute computation in parallel, maximizing the use of multicore architectures. In this context, the Collectors.averagingInt()
, Collectors.averagingLong()
, and Collectors.averagingDouble()
methods provide a way to calculate the average in a thread-safe manner, taking full advantage of parallel processing if a parallel stream is used.
Using these methods, you can calculate the average of a large set of numbers without having to manually implement the average calculation or worrying about synchronization in a parallel computation environment.
Here is how you can do it:
- Average of int numbers
To calculate the average of int
numbers, you can use Collectors.averagingInt()
.
Assuming we have a list of integers named numbers
:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
We can calculate the average using the following code:
package org.kodejava.stream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class AveragingInt {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Double average = numbers.stream()
.collect(Collectors.averagingInt(Integer::intValue));
System.out.println("Average value: " + average);
}
}
- Average of long numbers
To calculate the average of long
numbers, you can use Collectors.averagingLong()
.
package org.kodejava.stream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class AveragingLong {
public static void main(String[] args) {
List<Long> longNumbers = Arrays.asList(1L, 2L, 3L, 4L, 5L);
Double longAverage = longNumbers.stream()
.collect(Collectors.averagingLong(Long::longValue));
System.out.println("Average value: " + longAverage);
}
}
- Average of double numbers
To calculate the average of double numbers, you can use Collectors.averagingDouble()
.
package org.kodejava.stream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class AveragingDouble {
public static void main(String[] args) {
List<Double> doubleNumbers = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0);
Double doubleAverage = doubleNumbers.stream()
.collect(Collectors.averagingDouble(Double::doubleValue));
System.out.println("Average value: " + doubleAverage);
}
}
The result of these averaging collectors will be a Double
variable holding the average value of the numbers.
Here is another example:
package org.kodejava.stream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ProductPriceAveraging {
public static void main(String[] args) {
List<Product> products = Arrays.asList(
new Product("Product 1", 10.0),
new Product("Product 2", 20.0),
new Product("Product 3", 30.0));
double averagePrice = products.stream()
.collect(Collectors.averagingDouble(Product::getPrice));
System.out.println("Average price: " + averagePrice);
}
}
class Product {
private final String name;
private final Double price;
public Product(String name, Double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public Double getPrice() {
return price;
}
}
In this example, the averagingDouble()
collector is used to find the average price of all products in the list. The Product::getPrice
method reference is used to tell the collector to use the getPrice()
method of the Product
class to retrieve the value for each product.
Overall, the averaging collectors in Java Stream API provide a concise, thread-safe, and efficient way to calculate averages over stream elements.
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024