Skip to content
Related Articles

Related Articles

Program to convert Java list of bytes to a Vector in Scala
  • Last Updated : 14 Jan, 2020

A java list of bytes can be converted to a Vector in Scala by utilizing toVector 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 Vector in Scala
  
// Importing Scala's JavaConversions object
import scala.collection.JavaConversions._
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating list of bytes in Java
        val list = new java.util.ArrayList[Byte]()
          
        // Adding characters to the list
        list.add(125)
        list.add(111)
          
        // Converting list to a Vector 
        val vec= list.toVector
          
        // Displays vector
        println(vec)
      
    }
}
Output:
Vector(125, 111)

Example:2#




// Scala program to convert Java list 
// to a Vector in Scala
  
// Importing Scala's JavaConversions object
import scala.collection.JavaConversions._
  
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating list of bytes in Java
        val list = new java.util.ArrayList[Byte]()
          
        // Adding bytes to the list
        list.add(-102)
        list.add(-123)
        list.add(88)
          
        // Converting list to a Vector 
        val vec= list.toVector
          
        // Displays vector
        println(vec)
          
    }
}
Output:
Vector(-102, -123, 88)

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 :