Skip to content
Related Articles

Related Articles

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

A java list of characters 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 character in Java
        val list = new java.util.ArrayList[Char]()
          
        // Adding characters to the list
        list.add('l')
        list.add('m')
        list.add('n')
          
        // Converting list to a String
        val str = list.toString
          
        // Displays output
        println(str)
      
    }
}
Output:
[l, m, n]

Here, the duplicate elements are eliminated and the resultant order of the elements in list is same as stated above.
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 character in Java
        val list = new java.util.ArrayList[Char]()
          
        // Adding characters to the list
        list.add('d')
        list.add('b')
        list.add('c')
          
        // Converting list to a String
        val str = list.toString
          
        // Displays output
        println(str)
      
    }
}
Output:
[d, b, c]

Here, the stated list is not in correct order so, the resultant output is also not in proper order.




My Personal Notes arrow_drop_up
Recommended Articles
Page :