收集器Averagint(ToIntFunction映射器) 方法用于查找参数中传递的整数的平均值。此方法返回一个收集器,该收集器生成应用于输入元素的整数值函数的算术平均值。如果没有元素作为输入元素传递,则此方法返回0。
null
此方法用于计算 算术平均数 是:

语法:
public static <T> Collector<T, ?, Double> averagingInt(ToIntFunction<? super T> mapper)
哪里
- 接口收集器< T、 A,R > :一种可变缩减操作,将输入元素累积到可变结果容器中,可以选择在处理完所有输入元素后将累积结果转换为最终表示形式。还原操作可以顺序执行,也可以并行执行。
- T: 还原操作的输入元素类型。
- A: 还原操作的可变累积类型。
- R: 还原操作的结果类型。
- 双人: Double类在对象中封装了一个基本类型Double的值。Double类型的对象包含一个Double类型的字段。
- ToIntFunction: 表示生成int值结果的函数。
参数: 此方法采用强制参数 制图员 这是一种 ToIntFunction .它是一个从流中提取int类型值的函数。
下面是Averagint()方法的示例:
项目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); } } |
输出:
4.0
项目2: 当没有输入元素作为参数传递给Averagint()方法时。
// 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); } } |
输出:
0.0
方案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); } } |
输出:
8.5
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END