Q:

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

belongs to collection: Scala List Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to convert java list of floats to an Indxed Sequence in Scala

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Scala program to sort the items of List collection... >>
<< How to find the last element of a list in Scala?...