Scala Stack ++:() method with example
In Scala, scala.collection.mutable implements Stack data structure. The ++: method is similar to ++ method in Stack which gives a new stack with elements from the left hand operand followed by the elements from the right hand operand. But with the difference that in ++:() right operand determines the type of the resulting collection rather than the left one.
Method Definition – def ++:[B >: A, That](that: collection.Traversable[B])
Returns – A new stack which contains all elements of this stack followed by all elements of traversable.
Example #1:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val q 1 = Stack( 1 , 2 , 3 , 4 , 5 )
val q 2 = Stack( 11 , 12 , 13 , 14 , 15 )
val result = q 1 .++ : (q 2 )
print(result)
}
}
|
Output:
Stack(11, 12, 13, 14, 15, 1, 2, 3, 4, 5)
Example #2:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val q 1 = List( 1 , 2 , 3 )
val q 2 = Stack( "for" , "geeks" )
val result = q 1 ++ : q 2
print(result)
}
}
|
Output:
Stack(1, 2, 3, for, geeks)
Last Updated :
02 Nov, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...