Scala Stack :+() method with example
Last Updated :
02 Nov, 2019
In Scala, scala.collection.mutable implements Stack data structure. The :+ method is used to create a copy of this stack with an element appended.
Method Definition – def :+(elem: A)
Returns – a new stack consisting of all elements of this stack followed by elem.
Example #1:
import scala.collection.mutable._
object GfG
{
def main(args:Array[String])
{
val q2 = List(11, 12, 13, 14, 15)
val result = q2 :+ 100
print(result)
}
}
|
Output:
List(11, 12, 13, 14, 15, 100)
Example #2:
import scala.collection.mutable._
object GfG
{
def main(args:Array[String])
{
val st1 = Stack(1, 2, 3)
val st2 = Stack("Geeks", "For")
val result1 = st2 :+ st1
print(result1)
}
}
|
Output:
Stack(Geeks, For, Stack(1, 2, 3))
Share your thoughts in the comments
Please Login to comment...