Eevy Eevy - 1 year ago 73
Scala Question

Scala to Java8 stream compatibility issue


Files.walk(Paths.get("")).forEach(x =>


Error:(21, 16) missing parameter type
.forEach(x =>

and (java8)

Files.walk(Paths.get("")).forEach(x -> System.out.println(x.toString()));

works fine

What's wrong?

Answer Source

stream.forEach(x -> foo()) in java is syntactic sugar for

  new Consumer<Path> { public void accept(Path x) { foo(); } }

This is not at all the same as x => ... in scala, which is an instance of Function[Path,Unit].

Try this;

  .forEach(new Consumer[Path] { def accept(s: Path) = println(s) })
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download