繁体   English   中英

如何将方法作为参数传入

[英]How to pass in method as a parameter

我有一堆基本上做同样事情的方法:根据不同方法返回的值选择 class 的前 N 个实例,所有这些实例都返回双精度值。

例如,对于实现以下接口的 class 的对象:

interface A {
    Double getSalary();
    Double getAge();
    Double getHeight();
}

我想 select N 个对象,每个方法返回的值最高。

现在我有3种方法:

List<A> getTopItemsBySalary(List<A> elements);
List<A> getTopItemsByAge(List<A> elements);
List<A> getTopItemsByHeight(List<A> elements);

有这个身体:

List<A> getTopItemsBySalary(List<A> elements, int n) {
    return elements.stream()
              .filter(a -> a.getSalary() != null)                 
              .sorted(Comparator.comparingDouble(A::getSalary).reversed())
              .limit(n)
              .collect(Collectors.toList());
}

我怎样才能传入方法并且只有一种方法?

您可以使用将A转换为DoubleFunction ,例如:

List<A> getTopItems(List<A> elements, Function<A, Double> mapper, int n) {
    return elements.stream()
              .filter(a -> null != mapper.apply(a))                 
              .sorted(Comparator.<A>comparingDouble(a -> mapper.apply(a))
                      .reversed())
              .limit(n)
              .collect(Collectors.toList());
}

您可以使用以下方法调用它:

List<A> top10BySalary = getTopItems(list, A::getSalary, 10);
List<A> top10ByAge = getTopItems(list, A::getAge, 10);

如果您的 getter 应该总是返回一个非空值,那么ToDoubleFunction是一种更好的使用类型(但如果您的Double返回值可能为空值,它将不起作用):

List<A> getTopItems(List<A> elements, ToDoubleFunction<A> mapper, int n) {
    return elements.stream()
              .sorted(Comparator.comparingDouble(mapper).reversed())
              .limit(n)
              .collect(Collectors.toList());
}

我认为您可以更改 function 名称并添加 if 条件:

List<A> getTopItems(List<A> elements, int n, String byWhat) {
if (byWhat.equals("Salary"))
    return elements.stream()
              .filter(a -> a.getSalary() != null)                 
              .sorted(Comparator.comparingDouble(A::getSalary).reversed())
              .limit(n)
              .collect(Collectors.toList());
if (byWhat.equals("Height"))
    return elements.stream()
              .filter(a -> a.getHeight() != null)                 
              .sorted(Comparator.comparingDouble(A::getHeight).reversed())
              .limit(n)
              .collect(Collectors.toList());
if (byWhat.equals("Age"))
    return elements.stream()
              .filter(a -> a.getAge() != null)                 
              .sorted(Comparator.comparingDouble(A::getAge).reversed())
              .limit(n)
              .collect(Collectors.toList());

}

您可以将字段名称传递给通用 getTopItems function 并使用java.beans.PropertyDescriptor

List<A> getTopItemsByField(List<A> elements, String field) {
PropertyDescriptor pd = new PropertyDescriptor(field, A.class);
Method getter = pd.getReadMethod();
return elements.stream()
          .filter(a -> getter(a) != null)                 
          .sorted(Comparator.comparingDouble(a->a.getter(a)).reversed())
          .limit(n)
          .collect(Collectors.toList());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM