简体   繁体   English

何时在lambda表达式中指定参数类型是必要的

[英]When is specifying parameter types in lambda expressions necessary

Currently, I see no difference in the following Java 8 lambda expressions: 目前,我发现以下Java 8 lambda表达式没有区别:

Parameter types specified : 指定的参数类型:

Arrays.sort(strArray, (String s1, String s2) -> s2.length() - s1.length());

Parameter types omitted : 省略参数类型:

Arrays.sort(strArray, (s1, s2) -> s2.length() - s1.length());

EDIT: In general, what are the pros and cons for specifying parameter types in Java 8 lambda expressions? 编辑:在一般情况下,是什么在Java 8 Lambda表达式指定参数类型的利弊 And when is it necessary to specify? 什么时候需要指定?

I've thought of few possible reasons (but I'm sure there are more): 我想到了几个可能的原因(但我确信还有更多):

  • additional type safety checks when specified 指定时的附加型安全检查
  • better code readability 更好的代码可读性

There is no difference. 没有区别。 It's up to you what the tradeoff is. 这取决于你的权衡。 But frankly, you're better off writing neither of these; 但坦率地说,你最好不要写这些; instead, import static java.util.Comparator.comparingInt and do Arrays.sort(strArray, comparingInt(String::length)) . 相反, import static java.util.Comparator.comparingInt并执行Arrays.sort(strArray, comparingInt(String::length)) The first version isn't exactly more type-safe; 第一个版本不是更加类型安全; the second version will infer exactly the same type information and enforce it exactly as much. 第二个版本将推断完全相同的类型信息并完全执行它。

The lambda function element type is decided by the operate element type.( strArray ): lambda函数元素类型由操作元素类型决定。( strArray ):

public static <T> void sort(T[] a, Comparator<? super T> c) {
} 

the comparator type is generics and it's decided by your passed parameter type. 比较器类型是generics ,它由传递的参数类型决定。

The other lambda function is also same: 另一个lambda函数也是一样的:

<R> Stream<R> map(Function<? super T, ? extends R> mapper);
T reduce(T identity, BinaryOperator<T> accumulator);
...

so specify parameter type or not, the compiler already has known what's the parameter type should be. 所以指定参数类型与否,编译器已经知道参数类型应该是什么。

Going off of what @louis-wasserman said in the comments, there is another good example of an instance where you might want to ask that question: 关于@ louis-wasserman在评论中所说的,还有一个很好的例子,你可能想问这个问题:

// DOES NOT compile
Arrays.sort(array, Comparator.comparingInts(str -> str.length()).reversed());

// DOES compile
Arrays.sort(array, Comparator.comparingInts((String str) -> str.length()).reversed());

The way I understand it, Comparator.reversed() requires some sort of contract as to how the collection is inherently ordered. 我理解它的方式, Comparator.reversed()需要某种合同来确定集合本身是如何排序的。 Without knowing the type of str it will not know the return type of .length() , thus violating this contract. 在不知道str的类型的情况下,它不会知道.length()的返回类型,从而违反了这个合同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 何时需要为 collections 指定泛型类型? - When is specifying generic types for collections necessary? 是否需要在Block Lambda Expressions中关闭Scanner? - Is it necessary to close Scanner inside Block Lambda Expressions? 添加到ArrayList时如何避免“lambda表达式中不兼容的参数类型”? - How to avoid “incompatible parameter types in lambda expression” when adding to an ArrayList? Lambda 表达式如何推断 Java 中的类型? - How do Lambda expressions infer types in Java? lambda 表达式中的参数类型不兼容 - Incompatible parameter types in lambda expression 使用lambda表达式将Java类作为函数参数 - Java class as a function parameter using lambda expressions Lambda表达式 - 不能将lambda参数设置为方法的参数 - Lambda expressions - can not set lambda parameter as argument to method 在解析byte []时是否真的需要指定String编码? - Is specifying String encoding when parsing byte[] really necessary? Java Lambda表达式语法的清晰度-省略参数数据类型 - Clarity on syntax of Java lambda expressions - omit parameter data type 在Lambda Expressions(Java)中,如何使用没有参数的表达式? - In Lambda Expressions (Java), how is an expression without parameter used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM