Groovy流为.peek和.map提供MissingMethodException

时间:2019-05-22 15:41:41

标签: 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;
    }

再加上所有通常的...

有什么建议吗?

1 个答案:

答案 0 :(得分: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中。这看起来与您正在运行的内容相同。

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