Program to convert Java Set of bytes to a Traversable in Scala
Last Updated :
14 Jan, 2020
A java Set of bytes can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, we 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#
import scala.collection.JavaConversions. _
object GfG
{
def main(args : Array[String])
{
val set = new java.util.HashSet[Byte]()
set.add( 123 )
set.add( 111 )
set.add( 127 )
val tra = set.toTraversable
println(tra)
}
}
|
Output:
Set(123, 111, 127)
Example:2#
import scala.collection.JavaConversions. _
object GfG
{
def main(args : Array[String])
{
val set = new java.util.HashSet[Byte]()
set.add(- 126 )
set.add(- 111 )
set.add(- 99 )
val tra = set.toTraversable
println(tra)
}
}
|
Output:
Set(-99, -126, -111)
Share your thoughts in the comments
Please Login to comment...