Open In App

Program to convert Java Set of doubles to a String in Scala

Improve
Improve
Like Article
Like
Save
Share
Report

A java Set 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 set 
// 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 set of doubles in Java
        val set = new java.util.HashSet[Double]()
          
        // Adding doubles to the set
        set.add(2.23)
        set.add(3.10)
        set.add(4.2)
          
        // Converting set to a String
        val str = set.toString
          
        // Displays output
        println(str)
      
    }
}


Output:

[3.1, 2.23, 4.2]

Example:2#




// Scala program to convert Java set 
// 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 set of doubles in Java
        val set = new java.util.HashSet[Double]()
          
        // Adding doubles to the set
        set.add(3.22)
        set.add(4.22)
        set.add(6.22)
          
        // Converting set to a String
        val str = set.toString
          
        // Displays output
        println(str)
      
    }
}


Output:

[4.22, 6.22, 3.22]


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