Thursday, July 4, 2024
HomeLanguagesJavaJava 8 | Collectors averagingInt() with Examples

Java 8 | Collectors averagingInt() with Examples

Collectors averagingInt(ToIntFunction<? super T> mapper) method is used to find the mean of the integers passed in the parameters. This method returns a Collector that produces the arithmetic mean of an integer-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:

  {\displaystyle A={\frac {1}{n}}\sum _{i=1}^{n}a_{i}={\frac {a_{1}+a_{2}+\cdots +a_{n}}{n}}}

Syntax:

public static 
   <T> Collector<T, ?, Double> 
     averagingInt(ToIntFunction<? super T> mapper)

where,

  • 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.
  • ToIntFunction: Represents a function that produces an int-valued result.

Parameters: This method takes a mandatory parameter mapper which is of type ToIntFunction. It is a Function which extracts an int type of value from a stream.

Below are examples to illustrate averagingInt() method:

Program 1:




// Java code to show the implementation of
// Collectors averagingInt(ToIntFunction 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 with numbers
        Stream<String> s = Stream.of("3", "4", "5");
  
        // using Collectors averagingInt(ToIntFunction mapper)
        // method to find arithmetic mean of inputs given
        double ans = s
                         .collect(Collectors
                                      .averagingInt(
                                          num -> Integer.parseInt(num)));
  
        // displaying the result
        System.out.println(ans);
    }
}


Output:

4.0

Program 2: When no input element is passed as the parameter to averagingInt() method.




// Java code to show the implementation of
// Collectors averagingInt(ToIntFunction mapper) function
  
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a empty string stream
        Stream<String> s = Stream.of();
  
        // using Collectors averagingInt(ToIntFunction mapper)
        // method to find arithmetic mean of inputs given
        double ans = s
                         .collect(Collectors
                                      .averagingInt(
                                          num -> Integer.parseInt(num)));
  
        // displaying the result
        System.out.println(ans);
    }
}


Output:

0.0

Program 3:




// Java code to show the implementation of
// Collectors averagingInt(ToIntFunction 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 averagingInt(ToIntFunction mapper)
        // method to find arithmetic mean of inputs given
        double ans = s
                         .collect(Collectors
                                      .averagingInt(
                                          num -> Integer.parseInt(num)));
  
        // displaying the result
        System.out.println(ans);
    }
}


Output:

8.5

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments