A java list can be converted to a String in Scala by utilizing toString method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur.
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 in Java val list = new java.util.ArrayList[Int]() // Adding integers to the list list.add(5) list.add(6) list.add(7) // Converting list to a String val str = list.toString // Displays String println(str) } } |
[5, 6, 7]
Therefore, a String is returned. Here, firstly a list is created where, the int elements are added to it utilizing add method. After that toString method is utilized in order to convert the stated list to a String.
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 in Java val list = new java.util.ArrayList[Int]() // Adding integers to the list list.add(5) list.add(4) list.add(6) // Converting list to a String val str = list.toString // Displays String println(str) } } |
[5, 4, 6]
It is same as above example but here the proper order of the list is not maintained so, the output obtained is also not in a proper order.
Recommended Posts:
- Program to convert Java list of characters to a String in Scala
- Program to convert Java list of strings to a String in Scala
- Program to convert Java list of floats to a String in Scala
- Program to convert Java list of doubles to a String in Scala
- Program to convert Java list of shorts to a String in Scala
- Program to convert Java list of bytes to a String in Scala
- Program to convert Java list to an iterator in Scala
- Program to convert Java list to Set in Scala
- Program to convert Java Set to List in Scala
- Program to convert Java list to Stream in Scala
- Program to convert Java list to Seq in Scala
- Program to convert Java list to a Vector in Scala
- Program to convert Java list to Traversable in Scala
- Program to convert Java list of characters to a Vector in Scala
- Program to convert Java list of Integer to an Iterable in Scala
- Program to convert Java list of characters to an Indexed Sequence in Scala
- Program to convert Java list of integers to an Indexed Sequence in Scala
- Program to convert Java list of Strings to an Indexed Sequence in Scala
- Program to convert Java list of Strings to an Iterable in Scala
- Program to convert Java list of characters to an Iterable in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

