Skip to content
Related Articles

Related Articles

Program to convert Java list of doubles to a String in Scala
  • Last Updated : 14 Jan, 2020

A java list of doubles can be converted to a String in Scala by utilizing toString 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 String 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(1.34)
list.add(9.01)
list.add(7.1)
  
// Converting list to a String
val str = list.toString
  
// Displays output
println(str)
  
}
}
Output:
[1.34, 9.01, 7.1]

Example:2#




// Scala program to convert Java list
// to a String 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(7.12)
list.add(6.12)
list.add(9.12)
  
// Converting list to a String
val str = list.toString
  
// Displays output
println(str)
  
}
}
Output:
[7.12, 6.12, 9.12]

Reference:
https://alvinalexander.com/scala/how-to-go-from-java-collections-convert-in-scala-interact.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :