The Scala Collection library is powerful and systematically distinguished between mutable and immutable.
Want to find out more? Check out this article by Shubham Dangare!
Scala includes an elegant and powerful collection library. They are easy to use, concise, safe fast and universal.
Scala collection is systematically distinguished between mutable and immutable. A mutable collection can be updated or extended in place. Immutable collection by contrast never changes but still you can remove add update but it will return a new collection
All collection classes are found in scala.collection or in it sub package mutable, immutable and generic by default collection always pick immutable
val list = List() // List(): scala.collection.immutable.List
A useful convention, if you want to use both mutable and immutable versions of collections, is to import just the package collection.mutable.
import scala.collection.mutable
At the top of the collection, hierarchy is trait Traversable. It’s only abstract operation is foreach and all other methods are concrete for examples
/**
map
xs map f (The collection obtained from applying the function f to every element in)
*/
val list = List(1, 2, 3)
list.map(print)
/**
flatMap
xs flatMap f (The collection obtained from applying the collection-valued function f to every element in xs and concatenating the results)
*/
val list = List(List(1, 2, 3),List(4,6,7))
list.flatMap(_.toList)
/**
toArray
xs toArray (Convert collection to Array)
*/
val list = List(List(1, 2, 3),List(4,6,7))
val array = list.toArray
array(0)
/**
toList
xs toList (Convert collection to List)
*/
val list = Array(1,2,3,4,5)
val array = list.toList
array(0)
//Also like wise for toIterable, toSeq, toIndexedSeq, toStream, toSet, toMap
/**
copyToBuffer
xs copyToBuffer buf Copies all elements of the collection to buffer buf
*/
import scala.collection.mutable.ListBuffer
val list = List(1, 2, 3)
val buf = new ListBuffer[Int]
list.copyToBuffer(buf)
/**
isEmpty
xs.isEmpty Tests whether the collection is empty
*/
List().isEmpty // return true
/**
Size
xs.size The number of elements in the collection
*/
List(1,2,3).size // return 3
/**
head
xs.head The first element of the collection (or, some element, if no order is defined).
*/
List(1, 2, 3).head // return 1
/**
headOption
xs headOption The first element of xs in an option value, or None if xs is empty
*/
List(1, 2, 3).headOption // return Some(1)
/**
last
xs last The last element of the collection (or, some element, if no order is defined)
*/
List(1, 2, 3).last // return 3
/**
lastOption
xs lastOption The last element of xs in an option value, or None if xs is empty
*/
List().lastOption // return None
/**
find
xs find p An option containing the first element in xs that satisfies p, or None if no element qualifies
*/
List(1, 2, 3).find(_ == 9) // return None
/**
tail
xs tail The rest of the collection except xs.head.
*/
val map = Map("name"->"Shubham","value"->12000)
Map.tail // Map(value -> 12000): scala.collection.immutable.Map
/**
last
xs last The rest of the collection except xs.last
*/
val map = Map("name"->"Shubham","value"->12000 ,"Desgination"->"trainee")
Map.last // (Desgination,trainee): scala.Tuple2
/**
filter
xs filter p The collection consisting of those elements of xs that satisfy the predicate p
*/
val map = Map("value1"-> 30000 ,"value2"->12000 ,"value3"->50000)
map.filter(t => t._2 > 12000)
// Map(value1 -> 30000, value3 -> 50000): scala.collection.immutable.Map
/**
splitAt
xs splitAt n Split xs at a position, giving the pair of collections (xs take n, xs drop n)
*/
val map = Map("value1"-> 30000 ,"value2"->12000 ,"value3"->50000)
map.splitAt(1)
//(Map(value1 -> 30000),Map(value2 -> 12000, value3 -> 50000)): scala.Tuple2
/**
exists
xs exists p A boolean indicating whether the predicate p holds for some element in xs
*/
val map = Map("value1"-> 30000 ,"value2"->12000 ,"value3"->50000)
map.exists(t => t._2 > 120000) // return false
/**
count
xs count The number of elements in xs that satisfy the predicate p
*/
val map = Map("value1"-> 30000 ,"value2"->12000 ,"value3"->50000)
map.count(t => t._2 > 12000) // 2
/**
foldLeft
xs.foldLeft(z)(op) => (z /: xs)(op) Apply binary operation op between successive elements of xs, going left to right and starting with z
Same as (z /: xs)(op)
*/
val map = Map("value1"-> 30000 ,"value2"->12000 ,"value3"->50000)
map.foldLeft(3){ case (a, (k, v)) => a+v } // return 92003
And there are many more methods which you can refer on https://docs.scala-lang.org/overviews/collections/trait-traversable.html
Trait Iterable
The next trait from the top in the collections hierarchy is Iterable. All methods in this trait are defined in terms of an abstract method, iterator, which yields the collection’s elements one by one. The foreach method from trait Traversable is implemented in Iterable in terms of an iterator. Here is the actual implementation:
def foreach[U](f: Elem => U): Unit = {
val it = iterator
while (it.hasNext) f(it.next())
}
Trait Iterable also adds some other methods to Traversable that can be implemented efficiently only if an iterator is available like takeRight, zip, zipAll, grouped
val xs = List(1, 2, 3, 4, 5)
val git = xs grouped 3 // git: Iterator[List[Int]] = non-empty iterator
git.next() // res3: List[Int] = List(1, 2, 3)
git.next() //res4: List[Int] = List(4, 5)
val sit = xs sliding 3 // git: Iterator[List[Int]] = non-empty iterator
sit.next() // res5: List[Int] = List(1, 2, 3)
sit.next() //res6: List[Int] = List(2, 3, 4)
sit.next() //res7: List[Int] = List(3, 4, 5)
/**
takeRight
xs takeRight n - A collection consisting of the last n elements of xs (or, some arbitrary n elements, if no order is defined)
*/
Set(1,2,3,4,1,2).takeRight(1) // return 4
/**
dropRight
xs dropRight n The rest of the collection except xs takeRight n
*/
Set(1,2,3,4,1,2).dropRight(1) // return Set(1, 2, 3)
/**
sameElements
xs sameElements ys A test whether xs and ys contain the same elements in the same order
*/
val xs = Set(5,4,3,2,1)
xs sameElements Set(1,2,3,4,5) // return true
/**
xs zip ys An iterable of pairs of corresponding elements from xs and ys
*/
val xs = Set(3,2,1)
xs zip Set(1,2,3) // Set((3,1), (2,2), (1,3))
And many more methods are there refer https://docs.scala-lang.org/overviews/collections/trait-iterable.html
So that’s it from this blog I hoped you enjoyed and learned something new we will be covering the further topic of Set, Map, Seq and their methods in another blog
keep reading!!!!!'
This article was written by Shubham Dangare and posted originally on Knoldus Blog.