相关疑难解决方法(0)

Java 8 lambdas,Function.identity()或t-> t

我对该Function.identity()方法的用法有疑问.

想象一下以下代码:

Arrays.asList("a", "b", "c")
          .stream()
          .map(Function.identity()) // <- This,
          .map(str -> str)          // <- is the same as this.
          .collect(Collectors.toMap(
                       Function.identity(), // <-- And this,
                       str -> str));        // <-- is the same as this.
Run Code Online (Sandbox Code Playgroud)

是否有任何理由你应该使用Function.identity()而不是str->str(反之亦然).我认为第二种选择更具可读性(当然是品味问题).但是,有没有"真正的"理由为什么应该首选?

java lambda java-8

211
推荐指数
3
解决办法
10万
查看次数

标签 统计

java ×1

java-8 ×1

lambda ×1