Program to convert Java List of floats to an Indexed Sequence in Scala

Here, we will see how to convert Java List of Floats to an Indexed Sequence in Scala?
Submitted by Shivang Yadav, on October 30, 2020 [Last updated : March 11, 2023]

Java List of Floats to an Indexed Sequence

Scala programming language is based on Java that enables it to be interoperable with java. As Scala code is compiled to bytecode which is the same as compiled by java.

List of Floats in Java

It is simply, a list in java that contains elements of float data type.

Syntax to create a list of float in java,

List<float> floatList = new ArrayList<float>()

Syntax to create a list of float in Scala,

val floatList = new java.util.ArrayList[Float]()

Indexed Sequence

It is a subtrait of the trait sequence that returns a vector which allows random and fast access to the elements.

Scala code to convert java list of floats to an Indxed Sequence

// Program to illustrate the conversion of java list 
// of floats to an indexed sequence in scala

import scala.collection.JavaConversions._

object myObject {  
    def main(args:Array[String]) { 
        val floatList = new java.util.ArrayList[Float]() 
        
        floatList.add(45.81f) 
        floatList.add(12.56f) 
        floatList.add(98.30f) 
        
        val indexSeq = floatList.toIndexedSeq 
        
        println("The converted indexed sequence is " + indexSeq) 
    } 
} 

Output

The converted indexed sequence is Vector(45.81, 12.56, 98.30)

Scala List Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.