Skip to content
Related Articles

Related Articles

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

A java list of bytes can be converted to a String in Scala by utilizing toString method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work.
Now, lets see some examples and then discuss how it works in details.
Example:1#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 of bytes in Java
        val list = new java.util.ArrayList[Byte]()
          
        // Adding bytes to the list
        list.add(123)
        list.add(13)
        list.add(-11)
          
        // Converting list to a String
        val str = list.toString
          
        // Displays output
        println(str)
          
    }
}

chevron_right


Output:

[123, 13, -11]

Example:2#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 of bytes in Java
        val list = new java.util.ArrayList[Byte]()
          
        // Adding bytes to the list
        list.add(-123)
        list.add(-124)
        list.add(-125)
          
        // Converting list to a String
        val str = list.toString
          
        // Displays output
        println(str)
      
    }
}

chevron_right


Output:

[-123, -124, -125]



My Personal Notes arrow_drop_up
Recommended Articles
Page :