A java list of doubles can be converted to an Iterable in Scala by utilizing toIterable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur.
Now, lets see some examples and then discuss how it works in details.
Example:1#
import scala.collection.JavaConversions. _
object GfG
{
def main(args : Array[String])
{
val list = new java.util.ArrayList[Double]()
list.add( 8.1 )
list.add( 9.1 )
list.add( 10.1 )
val iterab = list.toIterable
println(iterab)
}
}
|
Output:
Buffer(8.1, 9.1, 10.1)
Example:2#
import scala.collection.JavaConversions. _
object GfG
{
def main(args : Array[String])
{
val list = new java.util.ArrayList[Double]()
list.add( 3.3 )
list.add( 2.4 )
list.add( 1.1 )
val iterab = list.toIterable
println(iterab)
}
}
|
Output:
Buffer(3.3, 2.4, 1.1)