Open In App

Program to convert Java list of floats to Traversable in Scala

Improve
Improve
Like Article
Like
Save
Share
Report

A java list of floats can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work.
Now, lets see some examples and then discuss how it works in details.
Example:1#




// Scala program to convert Java list 
// to a Traversable in Scala
  
// Importing Scala's JavaConversions object
import scala.collection.JavaConversions._
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating list of floats in Java
        val list = new java.util.ArrayList[Float]()
          
        // Adding floats to the list
        list.add(1.2f)
        list.add(1.6f)
        list.add(5.3f)
          
        // Converting list to a Traversable 
        val tra= list.toTraversable
          
        // Displays traversable 
        println(tra)
      
    }
}


Output:

Buffer(1.2, 1.6, 5.3)

Example:2#




// Scala program to convert Java list
// to a Traversable in Scala
  
// Importing Scala's JavaConversions object
import scala.collection.JavaConversions._
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating list of floats in Java
        val list = new java.util.ArrayList[Float]()
          
        // Adding floats to the list
        list.add(4.8f)
        list.add(8.8f)
        list.add(7.8f)
          
        // Converting list to a Traversable 
        val tra= list.toTraversable
          
        // Displays traversable 
        println(tra)
      
    }
}


Output:

Buffer(4.8, 8.8, 7.8)


Last Updated : 14 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads