📜  Java|带有示例的收集器 averagingLong (ToLongFunction mapper)

📅  最后修改于: 2022-05-13 01:55:05.441000             🧑  作者: Mango

Java|带有示例的收集器 averagingLong (ToLongFunction mapper)

收集器 averagingLong(ToLongFunction mapper)方法用于查找传入参数的 long 值的平均值。此方法返回一个收集器,该收集器生成应用于输入元素的长值函数的算术平均值。如果没有元素作为输入元素传递,则此方法返回 0。

该方法用于计算算术平均值的公式为:

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

句法:

public static  
    Collector 
        averagingLong(ToLongFunction mapper)

其中条款如下:

  • 接口 Collector :一种可变归约操作,可将输入元素累积到可变结果容器中,在处理完所有输入元素后,可选择将累积结果转换为最终表示。归约操作可以顺序或并行执行。
    • T:归约操作的输入元素的类型。
    • A:归约操作的可变累积类型。
    • R:归约操作的结果类型。
  • Double: Double 类将原始类型 double 的值包装在一个对象中。 Double 类型的对象包含一个类型为 double 的单个字段。
  • ToLongFunction :表示产生长值结果的函数。

参数:此方法接受一个参数映射器,该映射器是使用 ToLongFunctions 将长值流转换为 Long 的。 ToLongFunction是一个函数,它在处理流的对象时提取长类型的值。

以下是说明 averagingLong() 方法的示例:

方案一:

// 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 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);
    }
}
输出:
4.0

方案二:

// 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 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);
    }
}
输出:
8.5

程序 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 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);
    }
}
输出:
0.0