Program to convert Java list of Integer to an Iterable in Scala
Last Updated :
31 Dec, 2019
A java list of Integer can be converted to an Iterable in Scala by utilizing toIterable method of Java in Scala. Here, we 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[Int]()
list.add( 8 )
list.add( 9 )
list.add( 10 )
val iterab = list.toIterable
println(iterab)
}
}
|
Therefore, a buffer is returned and the elements are present in the order of the stated list and also duplicate elements are not eliminated here.
Example:2#
import scala.collection.JavaConversions. _
object GfG
{
def main(args : Array[String])
{
val list = new java.util.ArrayList[Int]()
list.add( 3 )
list.add( 2 )
list.add( 1 )
val iterab = list.toIterable
println(iterab)
}
}
|
Here, the stated list is not in proper order so the resultant buffer is also not in proper order.
Share your thoughts in the comments
Please Login to comment...