我们正在尝试向collect
我们的扩展Java Stream API 添加重载方法,我们目前将其定义为:
interface ExtendedStream<R> extends Stream<R> {
<R1> R1 collect(SerializableSupplier<Collector<? super R, ?, R1>> supplier);
}
SerializableSupplier
被定义为:
interface SerializableSupplier<T> extends Serializable, Supplier<T> {
}
使用lambda调用此collect方法可以很好地工作,但使用方法引用调用它时将无法编译,并显示以下错误:
Error:(50, 72) java: incompatible types: cannot infer type-variable(s) R1,capture#1 of ?,T
(argument mismatch; bad return type in method reference
java.util.stream.Collector<T,capture#1 of ?,java.util.List<T>> cannot be converted to java.util.stream.Collector<? super java.util.Map.Entry<java.lang.Integer,java.lang.String>,?,R1>)
我在这里创建了一个独立的Java类,您可以将其加载到编译器中进行尝试。
我当前正在使用Java版本:
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
这是我们定义事物时的错误还是某些错误?
注意:我从IntelliJ和使用Maven进行编译时都收到此编译错误。我不使用Eclipse。
干杯,加尔德
这是一个错误,javac 8
因为它可以与javac-9
(使用最新版本)一起很好地进行编译-我只是找不到它,而我真的非常想这样做。
几乎像往常一样,添加更多类型信息可以解决此类问题。另外请注意,我已将您更改Comparator
为Comparator.comparingInt...
:
return entrySet.stream().sorted(
Comparator.comparingInt(Entry::getKey)).collect(
Collectors::<Map.Entry<Integer, String>> toList);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句