Java | Collectors averagingLong (ToLongFunction mapper) with Examples
Collectors averagingLong(ToLongFunction<? super T> mapper)
Syntax:
public static <T> Collector<T, ?, Double> averagingLong(ToLongFunction<? super T> mapper)
- 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:
mapper
ToLongFunction
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); } } |
Output:
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); } } |
Output:
8.5
Program 3:
// 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); } } |
Output:
0.0
Contact Us