Program to print Java List of characters in Scala

A java list of characters can be returned from a Scala program by writing a user defined method of Java in Scala. Here, we don’t even need to import any Scala’s JavaConversions object in order to make this conversions work.
Now, lets see some examples.
Example:1#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to print Java List 
// of characters in Scala
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a java method in Scala
        def result = {
          
                // Creating a java list of characters 
                val list = new java.util.ArrayList[Char]()
          
                // Adding string elements in the List
                list.add('g')
                list.add('f')
                list.add('g')
          
                    // Displays output
                    println(list)
                                }
          
        // Assigning result method to list
        val list = result
    }
}

chevron_right


Output:

[g, f, g]

Therefore, a list of characters is returned from a Java method. Here, we don’t need to import any object of Scala. In the above program a Java method is written in Scala program. Where, this method adds the characters elements of the list to the stated list one after another and then prints the results.
Example:2#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to print Java List 
// of characters in Scala
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a java method in Scala
        def result = {
          
                // Creating a java list of characters 
                val list = new java.util.ArrayList[Char]()
          
                // Adding string elements in the List
                list.add('l')
                list.add('m')
                list.add('n')
          
                    // Displays output
                    println(list)
                                }
          
        // Assigning result method to list
        val list = result
    }
}

chevron_right


Output:

[l, m, n]

It is same as above example but here one more element is added in the stated list and here proper order of the elements is not required.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.