Updated March 31, 2023
Introduction to Scala Stack
Scala Stack is also like another programming language that is used to store and retrieving of elements. Scala Stack work in LIFO order which means list in first out. If we add an element to the stack the last element will be removed first. It means removing elements only from one end that is ‘top’. In Scala, we have two types of stack available mutable and immutable. Mutable is considered when the element inside the stack objects are changed frequently and immutable is something that is once assigned cannot change itself.
How Stack work in Scala?
Stack follow list in first out approach, an element which is insert last which will be removed first. Stack is the part of data structure that allows us to store and retrieve elements also they provide us various methods to do operations on the stack.
In scala we can define stack in two ways:
1. Creating an empty stack
In this way, we can create a stack without assigning it value. Only we mentioned the data type which stack is going to hold.
Example:
varvariable_name = Stack[data_type]()
var stack1 = Stack[Int]()
Here we are defining the stack name and it will hold the elements of Int type.
2. Creating a stack which contain values
In this way, we can create a stack and assign them a value at the time of creation only without mentioning the data type.
Example:
varvariable_name = Stack(value1, value2, value3, ... so on..)
var stack1 = Stack("1", "2", "3", "4", "5", "so on ...")
Here we are creating a stack with the name stack1 and assigning value to it at the time of creation only.
Now we can have a look at the extended class, super types, and some known subclasses structure below:
1. Extended classes of scala stack
AbstractSeq[A] | Seq[A] | SeqLike[A, Stack[A]] | GenericTraversableTemplate[A, Stack] | Cloneable[Stack[A]] | Serializable |
2. Super type classes of scala stack
Serializable | Java.io. Serializable | AbstarctSeq[A] | Seq[A] | SeqLike |
Cloneable | Scala.cloneable | Java.lang.cloneable | Iterable | Traversable |
Mutable | Collection.AbstarctSeq | Collection.Seq | Collection.seqLike | Genseq |
GenSeqLike | Stack | PartialFunction | Collection.AbstarctItearble | IterableLike |
Equals | GenIterable | AbstarctTraversable | Collection.Tarversable | GenTraversable |
GenricTrversableTemplate | TraversableLike | GenTraversableLike | Parallelizable | ParseSeq |
TraversableOnce | FiletrMonadic | HasNewBuilder | AyRef | Any |
3. Some known subclasses
StackProxy | SynchronizedStack |
Scala Stack Methods
Given below are the scala stack methods:
1. push()
This method is used to add elements into the stack.
Example:
Code:
import scala.collection.mutable.Stack
object Main extends App{
// Your code here!
//creating stack object
var stack1 = Stack[Int]()
//initializing value
stack1.push(100)
stack1.push(300)
stack1.push(500)
stack1.push(700)
stack1.push(900)
//display output
println("Element inside stack are :::" )
println(stack1)
}
Output:
2. pop()
This method is used to remove the elements from the stack but it will start removing the element from top.
Example:
Code:
import scala.collection.mutable.Stack
object Main extends App{
// Your code here!
//creating stack object
var stack1 = Stack[Int]()
//initializing value
stack1.push(100)
stack1.push(300)
stack1.push(500)
stack1.push(700)
stack1.push(900)
//display output
println("Element inside stack are :::" )
println(stack1)
stack1.pop()
stack1.pop()
//after removing element from stack
println("after removing element stack is ::")
println(stack1)
}
Output:
3. size()
This method will return the size of the stack.
Example:
Code:
import scala.collection.mutable.Stack
object Main extends App{
// Your code here!
//creating stack object
var stack1 = Stack[Int]()
//initializing value
stack1.push(100)
stack1.push(300)
stack1.push(500)
stack1.push(700)
stack1.push(900)
var size1 = stack1.size
//display output
println("Element inside stack are :::" )
println(stack1)
println("size before :::" )
println(size1)
stack1.pop()
stack1.pop()
var size2 = stack1.size
//after removing element from stack
println("after removing element stack is ::")
println(stack1)
println("size aftre :::" )
println(size2)
}
Output:
4. isEmpty
This method will return true or false. If the stack is empty it will return true else false.
Example:
Code:
import scala.collection.mutable.Stack
object Main extends App{
// Your code here!
//creating stack object
var stack1 = Stack[Int]()
//initializing value
stack1.push(100)
stack1.push(300)
stack1.push(500)
stack1.push(700)
stack1.push(900)
var size1 = stack1.isEmpty
//display output
println("Element inside stack are :::" )
println(stack1)
println("stack before :::" )
println(size1)
stack1.pop()
stack1.pop()
stack1.pop()
stack1.pop()
stack1.pop()
var size2 = stack1.isEmpty
//after removing element from stack
println("after removing element stack is ::")
println(stack1)
println("size aftre :::" )
println(size2)
}
Output:
5. max()
This method will return the max element from the stack.
Example:
Code:
import scala.collection.mutable.Stack
object Main extends App{
// Your code here!
//creating stack object
var stack1 = Stack[Int]()
//initializing value
stack1.push(100)
stack1.push(300)
stack1.push(500)
stack1.push(700)
stack1.push(900)
var size1 = stack1.max
//display output
println("Element inside stack are :::" )
println(stack1)
println("stack max :::" )
println(size1)
}
Output:
6. min()
This method is used to find the minimum element from the stack.
Example:
Code:
import scala.collection.mutable.Stack
object Main extends App{
// Your code here!
//creating stack object
var stack1 = Stack[Int]()
//initializing value
stack1.push(100)
stack1.push(300)
stack1.push(500)
stack1.push(700)
stack1.push(900)
var size1 = stack1.min
//display output
println("Element inside stack are :::" )
println(stack1)
println("stack min :::" )
println(size1)
}
Output:
7. sum
This method is used to get the sum of all elements present in the stack.
Example:
Code:
import scala.collection.mutable.Stack
object Main extends App{
// Your code here!
//creating stack object
var stack1 = Stack[Int]()
//initializing value
stack1.push(100)
stack1.push(300)
stack1.push(500)
stack1.push(700)
stack1.push(900)
var size1 = stack1.sum
//display output
println("Element inside stack are :::" )
println(stack1)
println("stack sum :::" )
println(size1)
}
Output:
8. take()
This method returns the first element from the stack to the range specified.
Example:
Code:
import scala.collection.mutable.Stack
object Main extends App{
// Your code here!
//creating stack object
var stack1 = Stack[Int]()
//initializing value
stack1.push(100)
stack1.push(300)
stack1.push(500)
stack1.push(700)
stack1.push(900)
var size1 = stack1.take(3)
//display output
println("Element inside stack are :::" )
println(stack1)
println("stack take :::" )
println(size1)
}
Output:
9. forall
This method will check the condition specified is true for all elements or not. It will return true or false based on the result obtained after iteration.
Example:
Code:
import scala.collection.mutable.Stack
object Main extends App{
// Your code here!
//creating stack object
var stack1 = Stack[Int]()
//initializing value
stack1.push(100)
stack1.push(300)
stack1.push(500)
stack1.push(700)
stack1.push(900)
val stack2 = stack1.forall(x => {x % 2 == 0})
//display output
println("Element inside stack are :::" )
println(stack1)
println("stack take :::" )
println(stack2)
}
Output:
Conclusion
Scala Stack is used to store elements and provide us with various methods to perform operations on the element present in the stack. Stack data structure based on LIFO. Stack elements will always be added and removed from the top there is only one end.
Recommended Articles
This is a guide to Scala Stack. Here we discuss the introduction, how stack works in scala? along with methods respectively. You may also have a look at the following articles to learn more –