Benefits of Using Vector in Scala
Sequence
- A sequence is implemented by list(list is immutable)
- Sequence support mutability and immutability.
Note: When pass mutability package: import scala.collection.mutable.seq .It return array Buffer.
package Collection
class SequenceExampl { val seq1 = Seq(3,4,5,6,7) println(seq1) } object Seq1{ def main(args:Array[String]):Unit={ new SequenceExampl() } }
Output:
List(3, 4, 5, 6, 7)
Process finished with exit code 0
The list follows FIFO.
package Collection
class SequenceExampl { val seq1 = Seq(3,4,5,6,7) println(seq1) def checkElements() ={ seq1.foreach(ele =>println("Access of elements :" +ele)) println(seq1(4)) } } object Seq1 { def main(args: Array[String]): Unit = { new SequenceExampl().checkElements() } }
Output:
List(3, 4, 5, 6, 7)
7
Process finished with exit code 0
Example:
package Collection
import scala.collection.mutable.Seq class SequenceExampl { val seq1 = Seq(3,4,5,6,7) seq1(3) = 60 println(seq1) def checkElements() ={ seq1.foreach(ele =>println("Access of elements :" +ele)) println(seq1(4)) } } object Seq1 { def main(args: Array[String]): Unit = { new SequenceExampl().checkElements() } }
Output:
ArrayBuffer(3, 4, 5, 60, 7)
7
Vector:
- Vector is good for large elements.
- Vector is by default immutable.
package Collection
class VectorExample { val ve = Vector(23,24,25,26,27,28,29,30) println(ve) } object VecE{ def main(args:Array[String]):Unit={ new VectorExample() } }
Output:
Vector(23, 24, 25, 26, 27, 28, 29, 30)
Process finished with exit code 0
Explanation: Vector return vector. It is immutable.
Question: Can we access the index value?
package Collection
class VectorExample { val ve = Vector(23,24,25,26,27,28,29,30) println(ve) println("return the index value 4 :" +ve(4)) println("return the index value 5 :" +ve(5)) println("return the index value 6 :" +ve(6)) println("return the index value 7 :" +ve(7)) } object VecE{ def main(args:Array[String]):Unit={ new VectorExample() } }
Output:
Vector(23, 24, 25, 26, 27, 28, 29, 30)
Process finished with exit code 0
Example :
package Collection
class VectorExample { val ve = Vector(23,24,25,26,27,28,29,30) ve(4) =33 println(ve) println(ve(4)) } object VecE{ def main(args:Array[String]):Unit={ new VectorExample() } }
Output:
value update is not a member of scala.collection.immutable.Vector[Int]
did you mean updated?
ve(4) =33
package Collection
class VectorExample { val ve = Vector(23,24,25,26,27,28,29,30) val ve1 = ve:+(3,4) val ve2 = ve++ve1 def ve_fun()= { println("Size of ve2 :" + ve2.size) println("First values of ve2 :" + ve2.head) println("Reverse elements of ve2 : " +ve2.reverse) // println("Sorted(ASC) order : " + ve2.sorted) //reverse is not working as ve2 elements (3,4) it's not a separate elements it's single elements and during running its not getting understand so throw error } println(ve) } object VecE { def main(args: Array[String]): Unit = { new VectorExample().ve_fun() } }
Output:
Vector(23, 24, 25, 26, 27, 28, 29, 30)
Size of ve2 :17
First values of ve2 :23
Reverse elements of ve2 : Vector((3,4), 30, 29, 28, 27, 26, 25, 24, 23, 30, 29, 28, 27, 26, 25, 24, 23)
Process finished with exit code 0
package Collection
class VectorExample { val ve = Vector(23,24,25,26,27,28,29,30) val ve1 = ve:+(3,4) val ve2 = ve++ve1 println("values of value ve2 :" +ve2) def ve_fun()= { println("Size of ve2 :" + ve2.size) println("First values of ve2 :" + ve2.head) println("Reverse elements of ve2 : " +ve2.reverse) //println("Sorted(ASC) order : " + ve2.sorted) } println(ve) } object VecE { def main(args: Array[String]): Unit = { new VectorExample().ve_fun() } }
Output:
values of value ve2 :Vector(23, 24, 25, 26, 27, 28, 29, 30, 23, 24, 25, 26, 27, 28, 29, 30, (3,4))
Vector(23, 24, 25, 26, 27, 28, 29, 30)
Size of ve2 :17
First values of ve2 :23
Reverse elements of ve2 : Vector((3,4), 30, 29, 28, 27, 26, 25, 24, 23, 30, 29, 28, 27, 26, 25, 24, 23)
Process finished with exit code 0
Example:
package Collection
class VectorExample { val ve = Vector(23,24,25,26,27,28,29,30) val ve1 = ve:+(3,4) val ve2 = ve++ve1 println("values of value ve2 :" +ve2) def ve_fun()= { println("Size of ve2 :" + ve2.size) println("First values of ve2 :" + ve2.head) println("Reverse elements of ve2 : " +ve2.reverse) println("Sorted(ASC) order : " + ve2.sorted) } println(ve) } object VecE { def main(args: Array[String]): Unit = { new VectorExample().ve_fun() } }
Output:
Error:
No implicit Ordering is defined for Any.
println(“Sorted(ASC) order: ” + ve2.sorted)
Important point: Index = length -1
Index always start from 0 to …
values of value ve2 :Vector(23, 24, 25, 26, 27, 28, 29, 30, 23, 24, 25, 26, 27, 28, 29, 30, (3,4))
sorted is not working here because sorted it not able to read out last element (3,4)
Solution how it can work the below program are given below
Example:
package Collection
class VectorExample { val ve = Vector(23,24,25,26,27,28,29,30) val ve1 = ve:+3 val ve2 = ve++ve1 println("values of value ve2 :" +ve2) def ve_fun()= { println("Size of ve2 :" + ve2.size) println("First values of ve2 :" + ve2.head) println("Reverse elements of ve2 : " +ve2.reverse) println("Sorted(ASC) order : " + ve2.sorted) } println(ve) } object VecE { def main(args: Array[String]): Unit = { new VectorExample().ve_fun() } }
Output:
values of value ve2 :Vector(23, 24, 25, 26, 27, 28, 29, 30, 23, 24, 25, 26, 27, 28, 29, 30, 3)
Vector(23, 24, 25, 26, 27, 28, 29, 30)
Size of ve2 :17
First values of ve2 :23
Reverse elements of ve2 : Vector(3, 30, 29, 28, 27, 26, 25, 24, 23, 30, 29, 28, 27, 26, 25, 24, 23)
Sorted(ASC) order : Vector(3, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30)
Process finished with exit code 0
Explanation: we can see that the sorted work.
Queue:
When we talk about data management FIFO (first in first out) and LIFO (last in last out) is important.
FIFO: queue
LIFO: stack
queue by default FIFO and it’s immutable.
It is immutable as well as mutable. But by default it is immutable. Using the queue, we have to import a package of import scala.collection.immutable.Queue
Example:
package Collection
import scala.collection.immutable.Queue object QueueExample { def main(args: Array[String]) = { val queue = Queue(23, 4, 5, 6, 7, 8, 8) println("value of queue : " + queue) println("first element of queue :" + queue(1)) } }
Output
value of queue : Queue(23, 4, 5, 6, 7, 8, 8)
first element of queue :4
Process finished with exit code 0
There are two methods to call queue
- dequeue method
- enqueue method
dequeue method: to delete the elements
enqueue method: it is for addition of elements
How dequeue work:
It follows FIFO order
suppose :(23, 4, 5, 6, 7, 8, 8)
After dequeue: (23, Queue(4, 5, 6, 7, 8, 8))
Again applying dequeue :(4, Queue(5, 6, 7, 8,8))
Example:
package Collection
import scala.collection.immutable.Queue object QueueExample { def main(args: Array[String]) = { var queue = Queue(23, 4, 5, 6, 7, 8, 8) println("value of queue : " + queue) println("first element of queue :" + queue(1)) val que1 = queue.enqueue(4) que1.foreach(ele => println("After enqueue : " + ele)) //print for que1 val que2 = queue.dequeue //It's an inside object so does not need to create instantiate println("After dequeue : " + que2) } }
Output:
value of queue : Queue(23, 4, 5, 6, 7, 8, 8)
first element of queue :4
Process finished with exit code 0
Stream:
“streaming” means something is moving.
means its ongoing business. It’s not historical that somebody is a store there. it’s just like a water
stream is a new concept in Scala it is not anywhere in java or python.
to work with a stream, we need to know the concept of laziness.
stream collection is implemented with lazy.
suppose in scala val a = 1 to 10
when we assign a is val then it occupies the RAM. Out of RAM, some space is occupied by variables. at one point in time, RAM gets full and it’s not managing well throwing an exception. It’s a wide issue since computer designed a computer. Then we have to know the algorithm.
lazy concept:
lazy is a keyword it only works with val .
#lazy val x = 1 to 1000
it does not take any memory.
suppose before assignment of lazy it takes some space 8GB RAM
after assignment does not take any space so it 0% utilization of CPU.
streaming is work like lazy
Question: What is the difference between function and method?
Answer: Method is the system has defined for you.
function means that we create
package Collection
object StreamExample { def main(args:Array[String])={ val str = (1 to 20 ).toStream println(str) println(str(5)) } }
Output:
Stream(1, <not computed>)
6
Process finished with exit code 0
Why streaming
Why streaming is called because control is in our hands. Because when we watch videos on Netflix, Amazon. Which video we want to buffer your memory will work based on that or your charges will start based on that if it’s not a monthly subscription.
So, streaming means always real-time whenever we say anything is streaming we will deal real-time data with a streaming business we cannot say 100% real-time because nothing is a hundred % we can say 80% real-time data.
How to create random values
Suppose we have random values. it always works with range.
when we want to use random values we have to use the below syntax
val str = 10 #:: 20 #:: 25 #:: Stream.empty
Example:
package Collection
object StreamExample { def main(args:Array[String])={ val str = 10 #:: 20 #:: 25 #:: Stream.empty println(str) } }
Output:
Stream(10, <not computed>)
Process finished with exit code 0
tuple:
- tuple support immutable
- tuple is a mixed element
Tuple does not have any name. As array has a name ar.
It does not support the index.
then how we can access the elements.
using (._1) .it will access the first element.
if we want to access all the elements one by one there is a function called .productIterator
Example: Benefits of Using Vector in Scala
package Collection
object TupleExample {
def main(args: Array[String]): Unit = {
val tup = (1, 1, "hey", 1.9, 20) println("all are the tuple elements :" + tup) println("first element of the tuple :" +tup._1) tup.productIterator.foreach(println) // .productIterator method to use to access one by one elements } }
Output: Benefits of Using Vector in Scala
all are the tuple elements :(1,1,hey,1.9,20)
first element of the tuple :1
1
1
hey
1.9
20
Process finished with exit code 0
Note:
try and exception try and catch block these are important for any programming language because when writing a code in production to write using try and catch method. spark is not a language it’s an API
Benefits of Using Vector in Scala