Program to convert Java list of doubles to Traversable in Scala
A java list of doubles can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work.
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( 3.5 )
list.add( 7.8 )
list.add( 9.2 )
val tra = list.toTraversable
println(tra)
}
}
|
Output:
Buffer(3.5, 7.8, 9.2)
Example:2#
import scala.collection.JavaConversions. _
object GfG
{
def main(args : Array[String])
{
val list = new java.util.ArrayList[Double]()
list.add( 5.8 )
list.add( 8.8 )
list.add( 1.8 )
val tra = list.toTraversable
println(tra)
}
}
|
Output:
Buffer(5.8, 8.8, 1.8)
Last Updated :
14 Jan, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...