Mutable and Immutable Collections in Scala
In Scala language collections framework id typically of 2 types.
- Mutable
- Immutable
In Mutable Collections we can change,add or remove the elements. But, in case of immutable collections , they never change. Though you can perform addition, deletion, updation etc operations on the immutable data set but each time it will return you a new variable.
Package:
Mutable: scala.collection.mutable
Immutable: scala.collection.immutable
If you don't explicitly specify any package name then by default it will point to immutable collection. Please find below some comparisons between two type of collections.
Properties
|
Mutable
|
Immutable
|
Manipulation of Variable
|
Possible
|
Not Possible
|
Speed
|
Faster
|
Slower
|
Memory Allocation
|
Once defined allocated into memory
|
In time of execution it uses memory
|
Data Security
|
Less
|
High
|
Use Case
|
In time of operations on data we should use this technique.
|
While exposing the data to end user/third party we should prefer this
technique.
|
No comments:
Post a Comment