Java 如何将方法作为参数传入

Java 如何将方法作为参数传入,java,Java,我有很多方法基本上都在做同样的事情:根据不同方法返回的值选择类的前N个实例,所有方法都返回双值 例如,对于实现以下接口的类的对象: interface A { Double getSalary(); Double getAge(); Double getHeight(); } 我想选择N个对象,每个方法返回的值最高 现在我有3种方法: List<A> getTopItemsBySalary(List<A> elements); List<A

我有很多方法基本上都在做同样的事情:根据不同方法返回的值选择类的前N个实例,所有方法都返回双值

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

interface A {
    Double getSalary();
    Double getAge();
    Double getHeight();
}
我想选择N个对象,每个方法返回的值最高

现在我有3种方法:

List<A> getTopItemsBySalary(List<A> elements);
List<A> getTopItemsByAge(List<A> elements);
List<A> getTopItemsByHeight(List<A> elements);
List getTopItemsBySalary(列表元素);
列表getTopItemsByAge(列表元素);
列表getTopItemsByHeight(列表元素);
该机构:

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());
}
List getTopItemsBySalary(列表元素,int n){
返回元素。stream()
.filter(a->a.getSalary()!=null)
.sorted(Comparator.comparingDouble(A::getSalary).reversed())
.限制(n)
.collect(Collectors.toList());
}

如何传入该方法而只使用一个方法?

您可以使用一个
函数
,将
a
转换为
Double
,例如:

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());
}

我认为您可以更改函数名并添加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());

}
List getTopItems(列表元素、int n、String byWhat){
如果(按什么等于(“工资”))
返回元素。stream()
.filter(a->a.getSalary()!=null)
.sorted(Comparator.comparingDouble(A::getSalary).reversed())
.限制(n)
.collect(Collectors.toList());
如果(通过What.equals(“高度”))
返回元素。stream()
.filter(a->a.getHeight()!=null)
.sorted(Comparator.comparingDouble(A::getHeight).reversed())
.限制(n)
.collect(Collectors.toList());
如果(按什么等于(“年龄”))
返回元素。stream()
.filter(a->a.getAge()!=null)
.sorted(Comparator.comparingDouble(A::getAge).reversed())
.限制(n)
.collect(Collectors.toList());
}

您可以将字段名传递给通用的getTopItems函数,并使用
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());
}
List getTopItemsByField(列表元素,字符串字段){
PropertyDescriptor pd=新的PropertyDescriptor(字段,A.class);
方法getter=pd.getReadMethod();
返回元素。stream()
.filter(a->getter(a)!=null)
.sorted(Comparator.comparingDouble(a->a.getter(a)).reversed())
.限制(n)
.collect(Collectors.toList());
}
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());

}
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());
}