ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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)
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.