2020-05-01

Useful Notes __ Scala Collections


The Scala collections library involves many classes and traits.The main trait is Iterable, which is the supertrait of both mutable and immutable variations of sequences (Seqs), sets, and maps. Sequences are ordered collections, such as Arrays and Lists. Sets contain at most one of each object, as determined by the == method. Maps contain a collection of keys mapped to values.
All collections are called as immutable by default, to call mutable type you should use scala.collection.mutable package where it is allowed.

Common syntax is available to create collection forms, by defining collection class in advance:


All collections support API Traversable by mentioning the type:



Here is the whole relational map of collections:

scala.collection.immutable

Scala Immutable Collections


scala.collection.mutable

Scala Mutable Collections

Iterators:

An iterator is not a collection, but rather a way to access the elements of a collection one by one. The two basic operations on an iterator are next()and hasNext. A call to next() will return the next element of the iterator and advance the state of the iterator. Calling next again on the same iterator will then yield the element one beyond the one returned previously. If there are no more elements to return, a call to next will throw a NoSuchElementException.


 

Lists:

Lists are immutable and one of the most used sequence type.
Structure of List: var list_example: List[T]=List(<elements>)
T=> means data type (String,Integer,Double,Any)
Some useful and most used list methods :


* All lists are built from two more fundamental constructors that are Nil and ::.Nil represents an empty list. :: also used for extending list items, ::: for merging the lists.
* Instead of defining the item, _ can be used too.
* Iterators can be applied to Lists.

Maps:

A Map is an Iterable consisting of pairs of keys and values (also named mappings or associations), similar to Dictionary in Python. Scala’s Predef object offers an implicit conversion that lets you write key -> value as an alternate syntax for the pair (key, value). For instance Map("x" -> 24, "y" -> 25, "z" -> 26) means exactly the same as Map(("x", 24), ("y", 25), ("z", 26)), but reads better. To access detail explanation, use the link. Map has both mutable and immutable types, to create mutable Map: scala.collection.mutable.Map, for immutable Maps just declare with var or val
Structure: var map_1:Map[String,Int]=Map("key_test"->value_int)
String: for key's data type, Int: for value's data type

Most used methods:



Sets:

Sets are Iterables that contain no duplicate elements, distinct elements and one data type only. Due to distinct and one data typed elements, adding or removing methods are used instead of updating.
Set is not an ordered collection - you can't get element by index.
Sets are available in both mutable and immutable forms, the latter one is declaring by default.
You could use head method to get single element from Set (it's not the first element, just some element).


Tuples:

A tuple isn't actually a collection; it's a series of classes named Tuple2, Tuple3, etc., through Tuple22.Contains both integer and string values together, keeps whole elements' original data types and available in immutable form.
Syntax:  var tuple_1=(1,"test") // with both integer and string values


Arrays:

Arrays allow you to hold a sequence of elements and efficiently access an element at an arbitrary position, both to get or update the element, with a zero-based index,Scala arrays also support all sequence operations.Scala arrays also support all sequence operations.Arrays are collection of mutable values and stores them by indexes.
Another form of Array is ArrayBuffer, which is a mutable sequence, so you can delete elements with the usual -=, --=, remove, and clear methods.The size of an Array can’t be changed, so you can not directly delete elements. You can reassign the elements in an Array, which has the effect of replacing them.
Use an Array if the length of Array is stable and fixed, and an ArrayBuffer if the length can vary.
Assume that the number of Arrayəs elements are not known, so using ArrayBuffer is recommended, afterward we can convert to Array type.
Difference between Array and ArrayBuffer:
  • ArrayBuffer is resizable, Array isn't. If you append an element to an ArrayBuffer, it gets larger. If you try to append an element to an Array, you get a new array. Therefore to use Arrays efficiently, you must know its size beforehand.
Difference between Arrays and Lists:
  • Lists are immutable whereas arrays are mutable in Scala.
  • Lists represents a linked list whereas arrays are flat
Most used methods:

To get broad info, visit Scala's official documentation page: https://docs.scala-lang.org/overviews/collections/overview.html

Extra info:
Scala data types:

No comments:

Post a Comment