Open In App

Java | Collectors averagingDouble() with Examples

Collectors averagingDouble(ToDoubleFunction<? super T> mapper) method is used to find the mean of the double values passed in the parameters. This method returns a Collector that produces the arithmetic mean of an double-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> 
        averagingDouble(ToDoubleFunction<? super T> mapper)
where the terms are as follows: Parameters: This method accepts a parameter mapper which is double-valued stream converted into Double using ToDoubleFunctions. ToDoubleFunction is a function which extracts a double type of value as it works on the objects of the stream. Below are examples to illustrate averagingDouble() method: Program 1:
// Java code to show the implementation of
// averagingDouble(ToDoubleFunction 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 averagingDouble(ToDoubleFunction mapper)
        // method to find arithmetic mean of inputs given
        double ans = s
                         .collect(Collectors
                                      .averagingDouble(
                                          num -> Double.parseDouble(num)));
  
        // displaying the result
        System.out.println(ans);
    }
}

                    
Output:
4.0
Program 2:
// Java code to show the implementation of
// averagingDouble(ToDoubleFunction 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 averagingDouble(ToDoubleFunction mapper)
        // method to find arithmetic mean of inputs given
        double ans = s
                         .collect(Collectors
                                      .averagingDouble(
                                          num -> Double.parseDouble(num)));
  
        // displaying the result
        System.out.println(ans);
    }
}

                    
Output:
8.5
Program 3: When no value is passed as parameter.
// Java code to show the implementation of
// averagingDouble(ToDoubleFunction 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 averagingDouble(ToDoubleFunction mapper)
        // method to find arithmetic mean of inputs given
        double ans = s
                         .collect(Collectors
                                      .averagingDouble(
                                          num -> Double.parseDouble(num)));
  
        // displaying the result
        System.out.println(ans);
    }
}

                    
Output:
0.0

Article Tags :