Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Stream from scala code

I'm trying to use Java 8 Stream from scala code as below and stuck with an compilation error.

any help appreciated!!

def sendRecord(record: String): Unit throws Exception

bufferedReader.lines().forEach(s => sendRecord(s))

Cannot resolve forEach with such signature, expect: Consumer[_ >: String], actual: (Nothing)

PS: though there is some indication that it is almost straight forward like https://gist.github.com/adriaanm/892d6063dd485d7dd221 it doesn't seem to work. I'm running Scala 2.11.8

like image 794
Somasundaram Sekar Avatar asked Oct 21 '25 18:10

Somasundaram Sekar


1 Answers

You can convert toiterator to iterate java Stream, like:

import scala.collection.JavaConverters._
bufferedReader.lines().iterator.asScala.forEach(s => sendRecord(s))
like image 62
chengpohi Avatar answered Oct 23 '25 06:10

chengpohi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!