Collectors averagingLong(ToLongFunction<? super T> mapper) method is used to find the mean of the long values passed in the parameters. This method returns a Collector that produces the arithmetic mean of an long-valued function applied to the input elements. If no elements are passed as the input elements, then this method returns 0.
The formula used by this method to calculate arithmetic mean is:

Syntax:
public static <T>
Collector<T, ?, Double>
averagingLong(ToLongFunction<? super T> mapper)
where the terms are as follows:
- Interface Collector<T, A, R>: A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel.
- T: The type of input elements to the reduction operation.
- A: The mutable accumulation type of the reduction operation.
- R: The result type of the reduction operation.
- Double: The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.
- ToLongFunction : Represents a function that produces a long-valued result.
Parameters: This method accepts a parameter mapper which is long-valued stream converted into Long using ToLongFunctions. ToLongFunction is a function which extracts a long type of value as it works on the objects of the stream.
Below are examples to illustrate averagingLong() method:
Program 1:
// Java code to show the implementation of // averagingLong(ToLongFunction mapper) function import java.util.stream.Collectors; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // creating a string stream Stream<String> s = Stream.of("3", "4", "5"); // using Collectors averagingLong(ToLongFunction mapper) // method to find arithmetic mean of inputs given double ans = s .collect(Collectors .averagingLong( num -> Long.parseLong(num))); // displaying the result System.out.println(ans); } } |
4.0
Program 2:
// Java code to show the implementation of // averagingLong(ToLongFunction mapper) function import java.util.stream.Collectors; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // creating a string stream Stream<String> s = Stream.of("7", "8", "9", "10"); // using Collectors averagingLong(ToLongFunction mapper) // method to find arithmetic mean of inputs given double ans = s .collect(Collectors .averagingLong( num -> Long.parseLong(num))); // displaying the result System.out.println(ans); } } |
8.5
Program 3: When no value is passed as parameter.
// Java code to show the implementation of // averagingLong(ToLongFunction mapper) function import java.util.stream.Collectors; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // creating a string stream Stream<String> s = Stream.of(); // using Collectors averagingLong(ToLongFunction mapper) // method to find arithmetic mean of inputs given double ans = s .collect(Collectors .averagingLong( num -> Long.parseLong(num))); // displaying the result System.out.println(ans); } } |
0.0
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- ToLongFunction Interface in Java with Examples
- Collectors toSet() in Java with Examples
- Java | Collectors averagingDouble() with Examples
- Java 8 | Collectors counting() with Examples
- Java 8 | Collectors averagingInt() with Examples
- Collectors toList() method in Java with Examples
- Collectors toMap() method in Java with Examples
- Collectors groupingBy() method in Java with Examples
- Collectors collectingAndThen() method in Java with Examples
- Java 8 Streams | Collectors.joining() method with Examples
- Java | Collectors minBy(Comparator comparator) with Examples
- Java | Collectors maxBy(Comparator comparator) with Examples
- DoubleStream map(DoubleUnaryOperator mapper) in Java
- IntStream map(IntUnaryOperator mapper) in Java
- LongStream map(LongUnaryOperator mapper) in Java
- DoubleStream flatMap(DoubleFunction mapper) in Java
- IntStream flatMap(IntFunction mapper) in Java
- LongStream flatMap(LongFunction mapper) in Java
- Java Collectors
- Collectors partitioningBy() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

