Scala immutable TreeSet toMap() method
Last Updated :
19 Apr, 2020
In Scala immutable TreeSet class the toMap() method is utilised to return a map consisting of all the elements of the TreeSet.
Method Definition: def toMap[T, U]: Map[T, U]
Return Type: It returns a map consisting of all the elements of the TreeSet.
Example #1:
import scala.collection.immutable. _
object GfG
{
def main(args : Array[String])
{
val t 1 = TreeSet(( 1 , 2 ), ( 3 , 4 ), ( 5 , 6 ))
println(t 1 )
val result = t 1 .toMap
print( "Elements in the Map: " + result)
}
}
|
Output:
TreeSet((1, 2), (3, 4), (5, 6))
Elements in the Map: Map(1 -> 2, 3 -> 4, 5 -> 6)
Example #2:
import scala.collection.immutable. _
object GfG
{
def main(args : Array[String])
{
val t 1 = TreeSet(( "u" , "a" ), ( "i" , "o" ), ( "e" , "f" ))
println(t 1 )
val result = t 1 .toMap
print( "Elements in the Map: " + result)
}
}
|
Output:
TreeSet((e, f), (i, o), (u, a))
Elements in the Map: Map(e -> f, i -> o, u -> a)
Share your thoughts in the comments
Please Login to comment...