3
Java 8 Lambda,Function.identity()或t-> t
Наэтотвопросестьответына 堆栈溢出нарусском:Лямбдыв爪哇8,чтолучшеиспользовать - Function.identity()или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. 是否有任何理由您应使用Function.identity()代替str->str(反之亦然)。我认为第二个选项更具可读性(当然是个人喜好了)。但是,有没有“真正的”理由为什么应该优先考虑呢?
240
java
lambda
java-8
java-stream