【问题标题】:Groovy stream gives MissingMethodException for .peek and .mapGroovy 流为 .peek 和 .map 提供 MissingMethodException
【发布时间】:2019-10-09 03:51:10
【问题描述】:

我有一些使用流的 groovy 代码,

List<MolportEntryVerification> verificationList = molportEntries.stream()
          .peek{if(i++%50 == 0){println(df.format(i/count))}}
          .map({entry -> verifier.verifyEntry(entry, new Standardizer(molportConfig),  new Standardizer(bciConfig))})
          .collect(Collectors.toList())

引起:

捕获:groovy.lang.MissingMethodException:没有方法签名:java.util.stream.ReferencePipeline$Head.peek() 适用于参数类型:(MolportFileVerification$_run_closure1) 值:[MolportFileVerification$_run_closure1@d62472f] 可能的解决方案:peek(java.util.function.Consumer)、grep()、sleep(long)、use([Ljava.lang.Object;)、grep(java.lang.Object)、wait()

长计数 = molportEntries.stream().count();

没有错误消息。

molportEntries 是 BasicMolportEntry 的列表,是一个简单的 java 类

public class BasicMolportEntry {
    public BasicMolportEntry(String molportId, String etxcId, String smiles) {
        this.molportId = molportId == null ? "" : molportId;
        this.etxcId = etxcId == null ? "" : etxcId;
        this.smiles = smiles;
    }

加上所有平常的...

有什么建议吗?

【问题讨论】:

  • 您使用的是哪个 Groovy 版本?你如何编译代码?这是编译时错误还是运行时错误?

标签: groovy


【解决方案1】:

以下代码:

class MolportEntryVerification {}
class BasicMolportEntry {}

def molportEntries = (1..200).collect { new BasicMolportEntry() }

def i = 0
List<MolportEntryVerification> result = molportEntries.stream()
  .peek { if(i++%50 == 0) { println(i-1) } }
  .map  { e -> new MolportEntryVerification() }
  .toList()

println "size: ${result.size()}"

在您的问题中模仿代码在 groovy 2.5.7 中可以正常工作,在 groovy 2.3.9 中因缺少 toList()(您的代码中没有,但它是收集器语法的简写)而中断,并与以下错误:

~> groovy solution.groovy

Caught: groovy.lang.MissingMethodException: No signature of method: java.util.stream.ReferencePipeline$Head.peek() is applicable for argument types: (solution$_run_closure2) values: [solution$_run_closure2@d706f19]
Possible solutions: peek(java.util.function.Consumer), grep(), sleep(long), use([Ljava.lang.Object;), grep(java.lang.Object), wait()
groovy.lang.MissingMethodException: No signature of method: java.util.stream.ReferencePipeline$Head.peek() is applicable for argument types: (solution$_run_closure2) values: [solution$_run_closure2@d706f19]
Possible solutions: peek(java.util.function.Consumer), grep(), sleep(long), use([Ljava.lang.Object;), grep(java.lang.Object), wait()
    at solution.run(solution.groovy:7)

在 groovy 1.8.8 中。这看起来与您遇到的相同 MissingMethodException 。

就像评论中提到的,这看起来像是一个 groovy 版本问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多