以下几行中,为什么第一个和最后一个允许?什么时候知道我是否可以用lambda使用双冒号?
List<String> l = new ArrayList<>();
l.stream().forEach(System.out::println);//compiles
l.stream().forEach(System.out.println(String::compareTo));//doesnt compile
l.stream().forEach(String::compareTo);// doesnt compile
String comparedWith = "";
l.stream().forEach(comparedWith::compareTo);//compiles why ?
编辑
如果compareTo需要一个的说法,应该不是也工作?
如果根据文档:
表示接受单个输入参数,并将 返回任何结果的操作。与大多数其他功能界面不同的是,期望{}代码 Consumer}通过副作用进行操作。
为什么第三个例子和'(String x,String y) - > x.compareTo(y)'是一样的,你能否详细说明一下? –
@estebanrincon,因为'''方法'compareTo'不是静态的,因此需要调用一个字符串;它也需要另一个字符串作为参数。 – immibis
啊,理解,谢谢! –