Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Collectors averagingDouble() method in Java 8
The averagingDouble() method of the Collectors class in Java 8 returns a Collector that is the arithmetic mean of a double-valued function applied to the input elements.
The syntax is as follows −
public static <T> Collector<T,?,Double> averagingDouble(ToDoubleFunction<? super T> mapper)
Here, the parameters −
T − Type of the input elements
mapper − Function extracting the property to be summed
Double − It wraps a value of the primitive type double in an object.
ToDoubleFunction − - A function that produces a double-valued result.
To work with Collectors class in Java, import the following package −
import java.util.stream.Collectors;
The following is an example to implement averagingDouble() method in Java −
Example
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
public static void main(String[] args) {
Stream<String> stream = Stream.of("20", "50", "75", "100", "150", "200");
double res = stream.collect(Collectors.averagingDouble(a -> Double.parseDouble(a)));
System.out.println("Arithmetic Mean of the stream elements = "+res);
}
}
Output
Arithmetic Mean of the stream elements = 99.16666666666667
Advertisements
