Home »
Scala
How to convert Java Set of floats to Vector in Scala?
Here, we are going to learn how to convert a Java Set of floats to Vector in Scala?
Submitted by Shivang Yadav, on January 05, 2021
Sets in java is a data structure to store unique elements.
Example:
set (5.1, 9.04, 23.5)
Vectors in Scala are immutable-indexed data structures that provide random access to the elements of the vectors.
Example:
vector(5.1, 9.04, 23.5)
Converting of data structure from one to another is possible and is done using the toVector method in Scala.
Syntax:
set.toVector
Program to illustrate the conversion of java set of floats to vector in Scala
import scala.collection.JavaConversions._
object myObject {
def main(args: Array[String]) {
val javaSet = new java.util.HashSet[Float]()
javaSet.add(5.1f)
javaSet.add(9.04f)
javaSet.add(23.5f)
println("Java Set : " + javaSet)
val scalaVector = set.toVector
println("Scala Vector : " + scalaVector)
}
}
Output:
Java Set : set(5.1, 9.04, 23.5)
Scala Vector: vector(5.1, 9.04, 23.5)