A java set 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#
// Scala program to convert Java set // to an Iterable in Scala // Importing Scala's JavaConversions object import scala.collection.JavaConversions. _
// Creating object object GfG
{ // Main method
def main(args : Array[String])
{
// Creating set of doubles in Java
val set = new java.util.HashSet[Double]()
// Adding doubles to the set
set.add( 3.56 )
set.add( 9.34 )
set.add( 11.67 )
// Converting set to an Iterable
val iterab = set.toIterable
// Displays output
println(iterab)
}
} |
Output:
Set(3.56, 9.34, 11.67)
Example:2#
// Scala program to convert Java set // to an Iterable in Scala // Importing Scala's JavaConversions object import scala.collection.JavaConversions. _
// Creating object object GfG
{ // Main method
def main(args : Array[String])
{
// Creating set of doubles in Java
val set = new java.util.HashSet[Double]()
// Adding doubles to the set
set.add( 11.22 )
set.add( 45.22 )
set.add( 69.22 )
// Converting set to an Iterable
val iterab = set.toIterable
// Displays output
println(iterab)
}
} |
Output:
Set(45.22, 69.22, 11.22)