Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 03 Jan, 2020

A java Set of shorts 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 shorts in Java
        val set = new java.util.HashSet[Short]()
          
        // Adding shorts to the set
        set.add(1000)
        set.add(-1000)
        set.add(111)
        set.add(-111)
          
        // Converting set to a String
        val str = set.toString
          
        // Displays output
        println(str)
      
    }
}
Output:
[-1000, 1000, -111, 111]

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 shorts in Java
        val set = new java.util.HashSet[Short]()
          
        // Adding shorts to the set
        set.add(-1000)
        set.add(1000)
        set.add(-111)
          
        // Converting set to a String
        val str = set.toString
          
        // Displays output
        println(str)
      
    }
}
Output:
[-1000, 1000, -111]



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!