Program to convert Java Set of characters to a Traversable in Scala
A java Set of characters can be converted to a Traversable collection in Scala by utilizing toTraversable 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#
import scala.collection.JavaConversions. _
object GfG
{
def main(args : Array[String])
{
val set = new java.util.HashSet[Char]()
set.add( 'x' )
set.add( 'y' )
set.add( 'z' )
val tra = set.toTraversable
println(tra)
}
}
|
Here, the stated output is in same order as stated in the given set.
Example:2#
import scala.collection.JavaConversions. _
object GfG
{
def main(args : Array[String])
{
val set = new java.util.HashSet[Char]()
set.add( 'g' )
set.add( 'f' )
set.add( 'e' )
val tra = set.toTraversable
println(tra)
}
}
|
Here, the stated set is not given in proper order but the resultant output is in proper order.
Last Updated :
03 Jan, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...