通过功能接口的默认方法可以链接。但是“问题”在于,当您返回合成表达式的右侧时,推理引擎没有足够的信息来确定左侧是相同的功能接口。
要提供该信息,您必须强制转换语句:
List<String> l = Collections.emptyList(); l.forEach(((Consumer<String>)System.out::println).andThen(System.out::println));
或先将其分配给变量:
Consumer<String> cons = System.out::println; Collections.<String>emptyList().forEach(cons.andThen(System.out::println));
或者,您也可以编写可以执行所需操作的静态帮助器方法
Collections.<String>emptyList().forEach(combine(System.out::println, System.out::println));static <T> Consumer<T> combine(Consumer<T>... consumers) { // exercise left to the reader}


