Groovy gdk名称空间冲突

时间:2015-11-02 23:04:42

标签: java groovy java-stream

当我在java 8 collect上使用Stream方法时,Groovy会抛出一个奇怪的错误。这是我的代码:

someStream.collect(Collectors.toList())

这就是错误:

Caught: groovy.lang.MissingMethodException: No signature of method: java.util.stream.LongPipeline$8.collect() is applicable for argument types: (java.util.stream.Collectors$CollectorImpl) values: [java.util.stream.Collectors$CollectorImpl@e41621]
Possible solutions: collect(), collect(groovy.lang.Closure), collect(java.util.Collection, groovy.lang.Closure), collect(java.util.function.Supplier, java.util.function.ObjLongConsumer, java.util.function.BiConsumer), close(), count()
groovy.lang.MissingMethodException: No signature of method: java.util.stream.LongPipeline$8.collect() is applicable for argument types: (java.util.stream.Collectors$CollectorImpl) values: [java.util.stream.Collectors$CollectorImpl@e41621]
Possible solutions: collect(), collect(groovy.lang.Closure), collect(java.util.Collection, groovy.lang.Closure), collect(java.util.function.Supplier, java.util.function.ObjLongConsumer, java.util.function.BiConsumer), close(), count()

我该如何防止这种情况?

1 个答案:

答案 0 :(得分:1)

您使用的是什么版本的Java 8我在the javadocs中看不到LongPipeline

我可以找到LongPipeline的唯一引用,只有以下收集方法:

collect(Supplier<R> resultFactory,
        ObjLongConsumer<R> accumulator,
        BiConsumer<R, R> combiner)

您可以在显示的例外中看到:

collect(java.util.function.Supplier, java.util.function.ObjLongConsumer, java.util.function.BiConsumer)

所以即使在Java中,你也无法调用

someStream.collect(Collectors.toList())