Scala Quick Notes :: Part - 2
Bhaskar S | 01/10/2015 |
Overview
In this part of the series, we will look at Scala collections such as List, Set, Map, and Tuple.
By default, we get an immutable version of these Scala collections (unlike in Java) staying true to the functional style of programming. If we desire the mutable version (like Java), we will have to explicitly refer to the package scala.collection.mutable.
Hands-on With Scala - II
The following is the Scala program named Sample04.scala:
Executing the program Sample04.scala results in the output:
Size of zippo - 0 Is zippo empty - true Size of brands - 5 Head item of brands - BMW Last item of brands - Mazda Tail of brands - List(Cadillac, Honda, Lexus, Mazda) 3rd item of brands - Honda Reverse of brands - List(Mazda, Lexus, Honda, Cadillac, BMW) Items of brands - List(BMW, Cadillac, Honda, Lexus, Mazda) Nissan appended to brands - List(BMW, Cadillac, Honda, Lexus, Mazda, Nissan) Audi prepended to brands - List(Audi, BMW, Cadillac, Honda, Lexus, Mazda) Audi prepended to brands (using ::) - List(Audi, BMW, Cadillac, Honda, Lexus, Mazda) Items in nums: 1 2 3 4 5 Drop first 2 items of nums - List(3, 4, 5) Drop lasst 2 items of nums - List(1, 2, 3) Items of evenNums - List(2, 4, 6, 8, 10) Items of oddNums - List(1, 3, 5, 7, 9) Items of evenNums and oddNums - List(2, 4, 6, 8, 10, 1, 3, 5, 7, 9) Items of evenNums and oddNums (using :::) - List(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)
The following section explains some of the aspects of the Scala program Sample04.scala:
The List operator :: is also called a cons operator and is used to prepend an element to a List. The operator :: is a method of its right operand
Ex: val nums = 1 :: 2 :: 3 :: 4 :: 5 :: Nil
Ex: "Audi" :: brands
Use the template List[T] :+ T to append an element of type T to the List[T]
Ex: brands :+ "Nissan"
Use the template T +: List[T] to prepend an element of type T to the List[T]. The operator +: is a method of its right operand
Ex: "Audi" +: brands
The List operator ++ or ::: is a List concatenation operator. It is used to concatenate two Lists
Ex: evenNums ++ oddNums
Ex: evenNums ::: oddNums
The following is the Scala program named Sample05.scala:
Executing the program Sample05.scala results in the output:
Size of zippo - 0 Is zippo empty - true Size of brands - 5 Head item of brands - Honda Last item of brands - Mazda Tail of brands - Set(Lexus, Cadillac, BMW, Mazda) Contains Audi in brands - false Contains Lexus in brands - true Items of brands - Set(Honda, Lexus, Cadillac, BMW, Mazda) Nissan added to brands - Set(Honda, Lexus, Cadillac, BMW, Nissan, Mazda) Lexus removed from brands - Set(Honda, Cadillac, BMW, Mazda) Items in moreBrands: Lexus, Mazda, Nissan, Toyota Drop first 2 items of moreBrands - Set(Nissan, Toyota) Drop lasst 2 items of moreBrands - Set(Lexus, Mazda) Items of brands and moreBrands - Set(Toyota, Honda, Lexus, Cadillac, BMW, Nissan, Mazda) Items of moreBrands removed from brands - Set(Honda, Cadillac, BMW) Items common to brands and moreBrands - Set(Lexus, Mazda)
The following section explains some of the aspects of the Scala program Sample05.scala:
Use the template Set[T] + T to add an element of type T to the Set[T]
Ex: brands + "Nissan"
Use the template Set[T] - T to remove an element of type T to the Set[T]
Ex: brands - "Lexus"
Use the template Set[T] ++ Set[T] to add a set of elements to the Set[T]
Ex: brands ++ moreBrands
Use the template Set[T] -- Set[T] to remove a set of elements from the Set[T]
Ex: brands -- moreBrands
Use the template Set[T] & Set[T] to return the intersection of two sets (elements common to both the sets)
Ex: brands & moreBrands
The following is the Scala program named Sample06.scala:
Executing the program Sample06.scala results in the output:
Size of zippo - 0 Is zippo empty - true Size of oneToThree - 3 Size of ThreeToSix - 4 Head item of oneToThree - (1,One) Last item of oneToThree - (3,Three) Tail of oneToThree - Map(2 -> Two, 3 -> Three) Contains 0 in oneToThree - false Contains 3 in oneToThree - true Get 0 from oneToThree - None Get 3 from oneToThree - Some(Three) Items of oneToThree - Map(1 -> One, 2 -> Two, 3 -> Three) Keys of oneToThree - Set(1, 2, 3) Zero added to oneToThree - Map(1 -> One, 2 -> Two, 3 -> Three, 0 -> Zero) Zero removed from oneToThree - Map(1 -> One, 2 -> Two, 3 -> Three) Items in ThreeToSix: 3 -> Three, 4 -> Four, 5 -> Five, 6 -> Six Drop first 2 items of ThreeToSix - Map(5 -> Five, 6 -> Six) Drop lasst 2 items of ThreeToSix - Map(3 -> Three, 4 -> Four) Items of oneToThree and ThreeToSix - Map(5 -> Five, 1 -> One, 6 -> Six, 2 -> Two, 3 -> Three, 4 -> Four) Items 5 and 6 removed from ThreeToSix - Map(3 -> Three, 4 -> Four)
The following section explains some of the aspects of the Scala program Sample06.scala:
Use the template K -> V to add an entry with a key of type K and a value of type V to the Map[K, V]
Ex: val oneToThree = Map[Int, String] (1 -> "One", 2 -> "Two", 3 -> "Three")
Use the template Map[K, V] + (K -> V) to add an new entry with a key of type K and a value of type V to the Map[K, V]
Ex: oneToThree + (0 -> "Zero")
Use the template Map[K, V] - K to remove an entry for the key of type K from the Map[K, V]
Ex: oneToThree - 0
Use the template Map[K, V] ++ Map[K, V] to add a map of entries to the Map[K, V]
Ex: oneToThree ++ ThreeToSix
Use the template Map[K, V] -- List[K] to remove a list of keys from the Map[K, V]
Ex: ThreeToSix -- List(5, 6)
The following is the Scala program named Sample07.scala:
Executing the program Sample07.scala results in the output:
Tuple contact - (Alice,alice@earth.com) First element of contact - Alice Second element of contact - alice@earth.com Tuple userWait - (Alice,4) First element of userWait - Alice Second element of userWait - 4
The following section explains some of the aspects of the Scala program Sample07.scala:
A Tuple is a type of collection that can contain different types of values in one object. Tuples are useful in a method call when we have a need to return multiple values from the method.
Use the template (V1, V2, V3) to create a Tuple containing three values - V1, V2, and V3
Ex: val contact = ("Alice", "alice@earth.com")
To access the individual values of a Tuple, one can use a dot, underscore, and the one-based index of the values in the Tuple. For example, the first value is _1, the second value is _2 and so on
Ex: printf("First element of contact - %s\n", contact._1); printf("Second element of contact - %s\n", contact._2)
References