Iterating over Java collections in Scala

前端 未结 9 1461
不思量自难忘°
不思量自难忘° 2020-11-28 02:19

I\'m writing some Scala code which uses the Apache POI API. I would like to iterate over the rows contained in the java.util.Iterator that I get from the Sheet

9条回答
  •  再見小時候
    2020-11-28 02:41

    If you would like to avoid the implicits in scala.collection.JavaConversions you can use scala.collection.JavaConverters to convert explicitly.

    scala> val l = new java.util.LinkedList[Int]()
    l: java.util.LinkedList[Int] = []
    
    scala> (1 to 10).foreach(l.add(_))
    
    scala> val i = l.iterator
    i: java.util.Iterator[Int] = java.util.LinkedList$ListItr@11eadcba
    
    scala> import scala.collection.JavaConverters._
    import scala.collection.JavaConverters._
    
    scala> i.asScala.mkString
    res10: String = 12345678910
    

    Note the use of the asScala method to convert the Java Iterator to a Scala Iterator.

    The JavaConverters have been available since Scala 2.8.1.

提交回复
热议问题