Skip to content
Related Articles
Open in App
Not now

Related Articles

Program to convert Java list of characters to a String in Scala

Improve Article
Save Article
Like Article
  • Last Updated : 14 Jan, 2020
Improve Article
Save Article
Like Article

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
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!