In Scala, scala.collection.mutable implements Stack data structure. The +: method is similar to ++ method in Stack which gives a copy of the stack with an element prepended. Note that the ending operators are right associative.
Method Definition – def +:(elem: A)
Returns – A new stack consisting of elem followed by all elements of this stack.
Example #1:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val q 1 = 1
val q 2 = Stack( "for" , "geeks" )
val result = q 1 + : q 2
print(result)
}
}
|
Output:
Stack(1, for, geeks)
Example #2:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val q 1 = List( 1 )
val q 2 = List( 11 , 12 , 13 , 14 , 15 )
val result = q 1 .+ : (q 2 )
print(result)
}
}
|
Output:
List(List(11, 12, 13, 14, 15), 1)