亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將方法作為參數傳入

如何將方法作為參數傳入

月關寶盒 2023-09-20 16:40:51
我有一堆基本上做同樣事情的方法:根據類的不同方法返回的值選擇類的前 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());}我如何傳入方法并且只有一種方法?
查看完整描述

3 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

您可以使用 aFunction轉換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());

}

您可以使用以下方式調用它:


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) {

    return elements.stream()

              .sorted(Comparator.comparingDouble(mapper).reversed())

              .limit(n)

              .collect(Collectors.toList());

}


查看完整回答
反對 回復 2023-09-20
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

您可以將字段名稱傳遞給通用 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());

}


查看完整回答
反對 回復 2023-09-20
?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

我認為你可以更改函數名稱并添加 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());


}


查看完整回答
反對 回復 2023-09-20
  • 3 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號