如何将方法作为参数传入

我有一堆基本上做同样事情的方法:根据类的不同方法返回的值选择类的前 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<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());

}

我如何传入方法并且只有一种方法?


月关宝盒
浏览 99回答 3
3回答

墨色风雨

您可以使用 aFunction转换A为Double,例如:List<A> getTopItems(List<A> elements, Function<A, Double> mapper, int n) {&nbsp; &nbsp; return elements.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(a -> null != mapper.apply(a))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted(Comparator.<A>comparingDouble(a -> mapper.apply(a))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .reversed())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .limit(n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());}您可以使用以下方式调用它:List<A> top10BySalary = getTopItems(list, A::getSalary, 10);List<A> top10ByAge = getTopItems(list, A::getAge, 10);如果您的 getter 预计始终返回非 null,那么这ToDoubleFunction是一个更好的使用类型(但如果您的返回值可能为 null,则它将不起作用Double):List<A> getTopItems(List<A> elements, ToDoubleFunction<A> mapper, int n) {&nbsp; &nbsp; return elements.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted(Comparator.comparingDouble(mapper).reversed())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .limit(n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());}

哔哔one

您可以将字段名称传递给通用 getTopItems 函数并使用java.beans.PropertyDescriptorList<A> getTopItemsByField(List<A> elements, String field) {PropertyDescriptor pd = new PropertyDescriptor(field, A.class);Method getter = pd.getReadMethod();return elements.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(a -> getter(a) != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted(Comparator.comparingDouble(a->a.getter(a)).reversed())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .limit(n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());}

慕婉清6462132

我认为你可以更改函数名称并添加 if 条件:List<A> getTopItems(List<A> elements, int n, String byWhat) {if (byWhat.equals("Salary"))&nbsp; &nbsp; return elements.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(a -> a.getSalary() != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted(Comparator.comparingDouble(A::getSalary).reversed())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .limit(n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());if (byWhat.equals("Height"))&nbsp; &nbsp; return elements.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(a -> a.getHeight() != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted(Comparator.comparingDouble(A::getHeight).reversed())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .limit(n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());if (byWhat.equals("Age"))&nbsp; &nbsp; return elements.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(a -> a.getAge() != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted(Comparator.comparingDouble(A::getAge).reversed())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .limit(n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java