Skip to content
Related Articles

Related Articles

Program to print Java List in Scala
  • Last Updated : 25 Nov, 2019

A java list 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#




// Scala program to print Java List 
// 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
         val list = new java.util.ArrayList[Int]()
  
         // Adding elements in the List
         list.add(11)
         list.add(12)
  
               // Displays output
               println(list)
                           }
  
// Assigning result method to list
val list = result
  }
}
Output:
[11, 12]

Therefore, a list 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 elements of the list to the stated list one after another and then prints the results.
Example:2#




// Scala program to print Java List 
// 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
         val list = new java.util.ArrayList[Int]()
  
         // Adding elements in the List
         list.add(11)
         list.add(12)
         list.add(17)
  
               // Displays output
               println(list)
                           }
  
// Assigning result method to list
val list = result
  }
}
Output:
[11, 12, 17]

It is same as above example but here one more element is added in the stated list.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :