6. Traversable
● An iterator is traversing object which allows one to walk
through the collection.
● Trait Traversable conceptualize the concept of an internal
iterator
● Traversable only method is foreach
● A client code can be passed to the foreach method which will
be executed on each element of the collection it is being
applied
● foreach is side effective
8. Iterable
● Iterable is an external iterator.
● An external iterator is used to traverse a collection with
custom logic.
● The trait Iterable provides a methods iterator which is used
to get the iterator.
● Iterator can be used as an object to traverse through the
collection.
● Iterable provides methods used to traverse and access the
element from the collection without worrying about the
implementation of the collection.
10. Seq
● Represents collections that has a sequential ordering.
● Defined in terms of the length and apply method
● apply can be used to index into the collection while length is
used to calculate size of the collection
● Entertain as many duplicate elements as possible but in an
particular sequence
11. Linear Seq
● Sequence that can be partitioned in a head and a tail
component.
● The LinearSeq trait provides methods such as isEmpty,
head and tail.
● The examples of Linear Sequences are Lists, Stacks etc
● Linear Sequence are best suited for recursive algorithms
● All of the operations defined in the trait has constant time
complexity of element access O(1)
12. Indexed Seq
● Provides great ease in random access of elements in
Sequences
● It achieves constant or near constant time complexity while
accessing random elements from the sequence.
● The examples of an Indexed Seq is a Vector which behaves
like an array, providing near constant operations on a
immutable Object
val mySeq = IndexedSeq("A","B","C")
mySeq: IndexedSeq[String] = Vector(A, B, C)
13. Indexed Seq
● Provides great ease in random access of elements in
Sequences
● It achieves constant or near constant time complexity while
accessing random elements from the sequence.
● The examples of an Indexed Seq is a Vector which behaves
like an array, providing near constant operations on a
immutable Object
val mySeq = IndexedSeq("A","B","C")
mySeq: IndexedSeq[String] = Vector(A, B, C)
15. Set
● Trait that represents an unordered collection of unique
elements
● Provides methods like contains, subsetOf and apply
methods
● Best suited for requirements like checking the existence of
an element in a collection, Lookup Tables etc
● Scala provides three types of mutable and immutable Sets
implementations, TreeSet, HashSet and BitSet.
16. TreeSet
● Implemented as a red black tree of elements.
● A Red black tree is a self balancing binary search tree.
● Best suited for requirements like checking the existence of
an element in a collection, Lookup Tables etc
● It guarantees search operations in O(log n) time.
● A TreeSet construction required an implicit Ordering for
comparison of elements during storage.
17. HashSet
● Implemented as tree of elements but uses Hashing to place
elements in the tree.
● A hash value is calculated for each value and the values with
same hash values are kept at the same tree node.
● HashSet has constant time operations like insertion and
element retrieval from the tree.
● HashSet whose hash function has less chance of collision
can outperform TreeSet in lookup and search operations.
18. BitSet
● Implemented as sequence of Long Values.
● Can store only non-negative integer values
● Often used to store sequences of bits and memory flags
● The maximum memory consumed by the BitSet is
dependent upon the largest number stored in it.
● The base trait BitSet is implemented in both version in scala,
i.e. scala.collection.immutable.BitSet and
scala.collection.mutable.BitSet.
19. BitSet: Example
val bits = scala.collection.immutable.BitSet.empty //Initializing the empty BitSet
bits: scala.collection.immutable.BitSet = BitSet()
val addedBits = bits + 3 + 4 + 7 //Adding elements to the bit set
addedBits: scala.collection.immutable.BitSet = BitSet(3, 4, 7)
val finalBits = addedBits + 7 + 9 //Adding more elements
finalBits: scala.collection.immutable.BitSet = BitSet(3, 4, 7, 9)
21. Map
● The Map trait denotes collection of key value pairs
● Only one value for a given key exists
●
Scala provides two map implementation, i.e. HashMap and
TreeMap.
●
A HashMap is preferred when the hashing algorithm is
enhanced and have less chances of collision.
●
TreeMap and HashMap shares same performance trade-offs
that is in TreeSet and HashSet
22. Map: Example
val author = Map("Scala"->"Martin Ordersky","Java"->"E Bala","C"->"Dennis Ritchie")
author: scala.collection.immutable.Map[String,String]
val prices = Map("C"->100,"Java"->200,"Scala"->1000)
prices: scala.collection.immutable.Map[String,Int]
val priceOfCBook = prices.get("C")
priceOfCBook: Option[Int] = Some(100)
val priceOfDBook = prices.get("D")
priceOfDBook: Option[Int] = None
24. Vector
● Vector is an IndexedSeq
● It is very efficient in random accessing the elements
●
It has random access complexity of log32 (N)
●
Vectors are implemented as a trie on the index position of
the element.
●
A trie is a tree where each child down the path share some
common characteristics
25. Trie: Example
A trie with branching factor of two, where each element is stored
as its binary position in the tree.
A trie with branching factor of two, where each element is stored
as its binary position in the tree.
26. Vector:Example
● Vector is a good balance of properties like fast random
access and fast random updates
●
It is safe to share across thread since it is immutable:
val vector = Vector(1, 2, 3)
vector: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)
val updatedVector = vector updated (2, 4)
updatedVector: scala.collection.immutable.Vector[Int] = Vector(1, 2, 4)
vector
res0: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)
28. Stream
● Stream is a lazy persistent collection
●
It can store infinite sequences without overflowing the
memory constraints.
●
Streams are just like Lists and composed of cons nodes and
empty stream but
●
Stream is not evaluated until the elements are needed
●
It stores function objects instead of actual elements to
compute the head element and the rest of the stream
31. ArrayBuffer
●
ArrayBuffer collection is a mutable Array defined in the
package scala.collection.mutable
●
Append, update and random access take constant time.
●
ArrayBuffer is created with some initial size given in the
constructor or with some default size defined.
●
When an element is added, the current size is checked,
●
if not sufficient a whole new array is allocated with a different
size and all previous element are moved into new array
along with new elements
32. ArrayBuffer
val arrayBuf:ArrayBuffer[Int] = new ArrayBuffer() //create empty array buffer
arrayBuf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
arrayBuf.append(4) //append element 4 to it
arrayBuf.append(7,8) //append element 7 and 8 to it
arrayBuf // print the arrayBuf now , It will be updated in place
res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4, 7, 8)
arrayBuf.size //Get the size of mutable array
res4: Int = 3 //It has grown to 4
34. Parallel Collections
●
Parallel collections in Scala aims to reduce the lower level
parallelism and programmer's level
●
It provides a high level abstraction of parallelism over normal
collections so they can be operated in parallel.
●
There are several wrapper methods available for normal
collection to get the parallel version.
●
One has to simply invoke the .par method on sequential
collections to operate it in parallel.