Guide to to Scala Collections
Scala consists of rich set of collection library. Collections consist of list of items. Those items can be sequenced in a way like Array, List, Tuple, Option, Map, iterator, sets etc.
Scala collections provide a rich set of data structures and operations for managing and manipulating data efficiently. Understanding Scala collections is fundamental for developing scalable and functional applications in Scala. Scala collections can be broadly categorized into mutable and immutable collections, each serving specific use cases based on mutability requirements.
Immutable collections in Scala, such as List, Set, Map, and others, are thread-safe and cannot be modified after creation. Operations on immutable collections return new collections without altering the original, promoting functional programming principles and ensuring predictable behavior.
Mutable collections provide mutability and direct modification capabilities, suitable for performance-critical scenarios where in-place modifications are necessary.
Guide to to Scala Collections
1) Array
a)
val n = new Array[Int](5) scala> val st = new Array[String](5) scala> val st = Array("Big","Data","s","s","t","u","u","w","w") valst1=Array("Hello","Prwatech","p","s","t","u","v","w","x","y","z","z","b","c","d","e")
b)
2) List
a)
scala> val lst = List(5,6)
scala> lst.head
scala> lst.tail
#Adding new elements using “::” from given head and tail
scala> 4 : : lst
b)
scala> var lst = List(5,6,”d”,”e”,”1″)
scala> lst.head
scala> lst.tail
scala> “e”::List(“5″,”1”)
scala> “4”::lst
3) Tuples
a)
scala> val a = (1,3,”Prwatech”,1.5)
scala> a._2
scala> a._3
scala> a._4
b)
scala> val x = (“ab”,3,”Prwa”,1.5,”i”)
scala> x._1
scala> x._4
scala> x._5
4) Map
a)
scala> val mapping = Map(“Tom” -> “Hardy”,”Christian” -> “Bale”)
scala> val mapping = scala.collection.mutable.Map(“Leonardo” -> “D”,”Brad” -> “B”)
scala> val x = mapping(“Tom”)
Accessing Map
scala> val mapping = Map(“Tom” -> “Hardy”,”Christian” -> “Bale”)
scala> val mapping = scala.collection.mutable.Map(“Leonardo” -> “D”,”Brad” -> “B”)
scala> val x = mapping(“Leonardo”)
Iterating Map elements
scala> for((d,b) <- mapping) yield(b,d)
b)
Scala> val mapping = Map(“Prwa” -> “utkarsh”,”Vinay” -> “Verma”)
Scala> val mapping = scala.collection.mutable.Map(“Vishal” -> “K”,”Vijay” -> “v”)
Scala> val x = mapping(“Prwa”)
Accessing Map elements
Scala> val mapping = Map(“Prwa” -> “utkarsh”,”Vinay” -> “Verma”)
Scala> val mapping = scala.collection.mutable.Map(“Vishal” -> “K”,”Vijay” -> “v”)
Scala> val x = mapping(“Vishal”)
Iterating Map elements
Scala> for((k,v) <- mapping) yield(v,k)
5) ArrayBuffer
a)
scala> import scala.collection.mutable.ArrayBuffer
scala> val x = ArrayBuffer[Int]()
scala> x+=1
scala> print(x++=Array(3,4,5))
scala> print(x++=Array(6))
scala> print(x+=(7))
b)
scala> import scala.collection.mutable.ArrayBuffer
scala> val x = ArrayBuffer[String]()
scala> x+=”a”
scala> print(x++=Array(“b”,”c”,”d”))
scala> print(x++=Array(“e”))
scala> print(x+=(“f”))
6) Set
A Scala Set is a type of collection that doesn't accept duplicates
a)
scala> var s=Set(1,2,3,3,2,5)
scala> var s:Set[Int]=Set(1,2,3,3,5)
scala> s.head
scala> s.tail
scala> s.isEmpty
val res3: Boolean = false
scala> var s1=Set(7,9,8,9)
//concatenating two sets s and s1
scala> s++s1
//find max no from s and s1 set
scala> s.max
scala> s1.max
//find min no from sets s1 and s
scala> s1.min
scala> s.min
b)
scala> val a=Set(9,9,3,3,2,”a”,7,”b”)
scala> a.head
scala> a.tail