Skip to content
Related Articles

Related Articles

Improve Article

Conversion from Java List to Scala Buffer

  • Last Updated : 21 Nov, 2019
Geek Week

A Java list can be converted to a Scala Buffer by importing JavaConversions.asScalaBuffer method. Here, we need to call asScalaBuffer method which has a java list as its argument. Therefore, this method returns a Scala Buffer.
Now, lets see some examples.
Example:1#




// Scala program of converting a Java list
// to a Scala Buffer
  
// Importing JavaConversions.asScalaBuffer
import scala.collection.JavaConversions.asScalaBuffer
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating Java List
        val jlist = java.util.Arrays.asList(11, 12, 13)
          
        // Converting from java list
        // to Scala Buffer
        val results= asScalaBuffer(jlist)
          
        // Displays results
        println(results)
    }
}
Output:
Buffer(11, 12, 13)

Therefore, a Scala Buffer is returned.
Example:2#




// Scala program of converting a Java list
// to a Scala Buffer
  
// Importing JavaConversions.asScalaBuffer
import scala.collection.JavaConversions.asScalaBuffer
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating Java List
        val jlist = java.util.Arrays.asList(6, 2, 8, 1)
          
        // Converting from java list
        // to Scala Buffer
        val results= asScalaBuffer(jlist)
          
        // Displays results
        println(results)
    }
}
Output:
Buffer(6, 2, 8, 1)



My Personal Notes arrow_drop_up
Recommended Articles
Page :