Scala TreeSet ++() method with example
In Scala TreeSet class
, the ++() method is utilized to add elements of one TreeSet to another TreeSet.
Method Definition: def ++(that: IterableOnce[A]): TreeSet[A]
Return Type: It returns a new TreeSet which contains all the element from both the given TreeSets.
Example #1:
// Scala program of ++() // method // Import TreeSet import scala.collection.mutable. _ // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating TreeSets val t 1 = TreeSet( "g" , "e" , "e" , "k" , "s" ) val t 2 = TreeSet( "a" , "e" , "i" , "o" , "u" ) // Print the TreeSets println(t 1 ) println(t 2 ) // Applying ++() method val result = t 1 ++ t 2 // Display output print( "Combined TreeSet: " + result) } } |
Output:
TreeSet(e, g, k, s) TreeSet(a, e, i, o, u) Combined TreeSet: TreeSet(a, e, g, i, k, o, s, u)
Example #2:
// Scala program of ++() // method // Import TreeSet import scala.collection.mutable. _ // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating TreeSets val t 1 = TreeSet( 1 , 6 , 3 , 2 , 5 ) val t 2 = TreeSet( 2 , 1 , 5 , 4 , 1 ) // Print the TreeSets println(t 1 ) println(t 2 ) // Applying ++() method val result = t 1 ++ t 2 // Display output print( "Combined TreeSet: " + result) } } |
Output:
TreeSet(1, 2, 3, 5, 6) TreeSet(1, 2, 4, 5) Combined TreeSet: TreeSet(1, 2, 3, 4, 5, 6)
Contact Us