Open In App
Related Articles

Program to convert Java list of doubles to an Iterable in Scala

Improve Article
Improve
Save Article
Save
Like Article
Like

A java list of doubles can be converted to an Iterable in Scala by utilizing toIterable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur.
Now, lets see some examples and then discuss how it works in details.
Example:1#




// Scala program to convert Java list 
// to an Iterable 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 doubles in Java
        val list = new java.util.ArrayList[Double]()
          
        // Adding doubles to the list
        list.add(8.1)
        list.add(9.1)
        list.add(10.1)
          
        // Converting list to an Iterable
        val iterab= list.toIterable
          
        // Displays output
        println(iterab)
      
    }
}

Output:

Buffer(8.1, 9.1, 10.1)

Example:2#




// Scala program to convert Java list 
// to an Iterable 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 doubles in Java
        val list = new java.util.ArrayList[Double]()
          
        // Adding doubles to the list
        list.add(3.3)
        list.add(2.4)
        list.add(1.1)
          
        // Converting list to an Iterable 
        val iterab= list.toIterable
          
        // Displays output
        println(iterab)
      
    }
}

Output:

Buffer(3.3, 2.4, 1.1)

Last Updated : 14 Jan, 2020
Like Article
Save Article
Similar Reads