Ben Allen
Ben Allen

Reputation: 1

Groovy stream gives MissingMethodException for .peek and .map

I have some groovy code that uses streams,

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())

Give rise to:

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

long count = molportEntries.stream().count();

works with no error message.

molportEntries is a list of BasicMolportEntry, which is a simple java class

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

plus all the usual...

Any suggestions?

Upvotes: 0

Views: 941

Answers (1)

Matias Bjarland
Matias Bjarland

Reputation: 4482

The following code:

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()}"

mimicking the code in your question works fine in groovy 2.5.7, breaks with a missing toList() (which you don't have in your code but is shorthand for the collector syntax) in groovy 2.3.9 and breaks with the following error:

~> 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)

in groovy 1.8.8. This looks like the same MissingMethodException as what you are running into.

Like mentioned in the comment, this looks like a groovy version problem.

Upvotes: 2

Related Questions