Home »
Scala
How to convert Java Set of floats to Vector in Scala?
By IncludeHelp Last updated : November 14, 2024
Java Sets
Sets in java is a data structure to store unique elements.
Example
set (5.1, 9.04, 23.5)
Scala Vectors
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)
Convert Java Set of floats to Vector
Converting of data structure from one (Java set) 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
import scala.jdk.CollectionConverters._
object myObject {
def main(args: Array[String]): Unit = {
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 = javaSet.asScala.toVector
println("Scala Vector : " + scalaVector)
}
}
Output
Java Set : set(5.1, 9.04, 23.5)
Scala Vector: vector(5.1, 9.04, 23.5)