Program to convert Java list of strings to a String in Scala
Last Updated :
14 Jan, 2020
A java list of strings 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#
import scala.collection.JavaConversions. _
object GfG
{
def main(args : Array[String])
{
val list = new java.util.ArrayList[String]()
list.add( "geeks" )
list.add( "for" )
list.add( "geeks" )
val str = list.toString
println(str)
}
}
|
Output:
[geeks, for, geeks]
Here, the duplicate elements are not eliminated and the resultant order of the elements in list is same as stated above.
Example:2#
import scala.collection.JavaConversions. _
object GfG
{
def main(args : Array[String])
{
val list = new java.util.ArrayList[String]()
list.add( "I" )
list.add( "am a" )
list.add( "writer" )
val str = list.toString
println(str)
}
}
|
Output:
[I, am a, writer]
Here, the stated list is not in correct order so, the resultant output is also not in proper order.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...