跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 为什么 Java 的 Function 接口可以接收 getMethod 这种方法签名的,用 lambda 就不行呢
未分類
14 1 月 2021

为什么 Java 的 Function 接口可以接收 getMethod 这种方法签名的,用 lambda 就不行呢

为什么 Java 的 Function 接口可以接收 getMethod 这种方法签名的,用 lambda 就不行呢

資深大佬 : ningmengmao 6

public class App {   public static void main(String[] args) {    Map<String, String> collect = Stream.of(new App(), new App(), new App())     .collect(Collectors.toMap(App::getString, (app) -> "aaa"));                  //  Map<String, String> collect = Stream.of(new App(), new App(), new App()) //    .collect(Collectors.toMap(() -> "str", (app) -> "aaa"));   }   public String getString() {   return "str";  } } 

我用方法引用可以编译运行,但是用 lambda 写法就不行了,这是为什么啊?

大佬有話說 (4)

  • 資深大佬 : chendy

    () -> “str”,这是个 Supplier (没参数又返回)
    然而要的是 Function (有参数有返回),所以应该是 app -> “str”

  • 主 資深大佬 : ningmengmao

    @chendy 但是那个 getString 方法的签名不也是() -> string 吗

  • 資深大佬 : Oktfolio

    上面那个是这样的啊,哪里一样了?

    Map<String, String> collect = Stream.of(new App(), new App(), new App())
    .collect(Collectors.toMap((it) -> {
    return it.getString();
    }, (app) -> “aaa”));

  • 主 資深大佬 : ningmengmao

    @Oktfolio 明白了,谢谢大佬

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具