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:
public static <T> Collector<T, ?, Double> averagingInt(ToIntFunction<? 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.
- 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 2: When no input element is passed as the parameter to averagingInt() method.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Collectors toSet() in Java with Examples
- Java 8 | Collectors counting() with Examples
- Java | Collectors averagingDouble() with Examples
- Collectors groupingBy() method in Java with Examples
- Collectors toList() method in Java with Examples
- Collectors collectingAndThen() method in Java with Examples
- Collectors toMap() method in Java with Examples
- Java 8 Streams | Collectors.joining() method with Examples
- Java | Collectors averagingLong (ToLongFunction mapper) with Examples
- Java | Collectors minBy(Comparator comparator) with Examples
- Java | Collectors maxBy(Comparator comparator) with Examples
- Collectors partitioningBy() method in Java
- Java Stream | Collectors toCollection() in Java
- Types of JVM Garbage Collectors in Java with implementation details
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples