Skip to content

Commit 58099d1

Browse files
committed
Maps in scala
Sets in scala
1 parent eb9c2f4 commit 58099d1

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

Collections/Maps.scala

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/***
2+
1. Maps store Key-Value Pairs
3+
2. Internally maps are collection of tuples and can be operated as such
4+
3. Symbols are like Strings but guaranteed to be Interned, perfect for Maps
5+
**/
6+
object Maps extends App
7+
{
8+
//Map can be created using tuples
9+
val m = Map.apply((1, "One"), (2, "Two"), (3, "Three"))
10+
println("m : " + m)
11+
val m2 = Map((1, "One"), (2, "Two"), (3, "Three"))
12+
13+
//declaring tuple using -> operator
14+
val tuple:(Int, String) = 3 -> "Three"
15+
println(tuple)
16+
17+
//declaring map using tuples with -> operator
18+
val m3 = Map(1 -> "One", 2 -> "Two", 3 -> "Three")
19+
println("m3 : " + m3)
20+
21+
println(m3.get(1)) //Some("One"). get method returns option
22+
println(m3.apply(1)) //"One"
23+
println(m3(1)) //"One"
24+
println(m3.get(4)) //None
25+
//println(m3(4)) //throws NoSuchElementException
26+
27+
println("m3.toList : " + m3.toList)
28+
println("m3.keys : " + m3.keys)
29+
println("m3.keySet : " + m3.keySet)
30+
println("m3.values.toSet : " + m3.values.toSet) //m3.values return MapLike.
31+
32+
val m4 = Map("One"->1, "Two"->2, "Three"->3)
33+
println("m4 : " + m4)
34+
println("m4(\"Two\") : " + m4("Two"))
35+
36+
// we can use Also Symbol as a key inside Map
37+
val sym = Symbol("Co")
38+
val sym2 = 'Co
39+
println("sym == sym2 :" + (sym == sym2))
40+
println("sym eq sym2 :" + (sym eq sym2))
41+
42+
val elements:Map[Symbol, String] = Map('Co -> "Cobalt", 'H -> "Hydrogen", 'Pb -> "Lead")
43+
println("elements('Co) :" + elements('Co))
44+
45+
println("elements - 'H : " + elements - 'H)
46+
47+
}
48+
49+
/**
50+
Sample Output
51+
m : Map(1 -> One, 2 -> Two, 3 -> Three)
52+
(3,Three)
53+
m3 : Map(1 -> One, 2 -> Two, 3 -> Three)
54+
Some(One)
55+
One
56+
One
57+
None
58+
m3.toList : List((1,One), (2,Two), (3,Three))
59+
m3.keys : Set(1, 2, 3)
60+
m3.keySet : Set(1, 2, 3)
61+
m3.values.toSet : Set(One, Two, Three)
62+
m4 : Map(One -> 1, Two -> 2, Three -> 3)
63+
m4("Two") : 2
64+
sym == sym2 :true
65+
sym eq sym2 :true
66+
elements('Co) :Cobalt
67+
68+
**/

Collections/Sets.scala

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
1. Set is a companion object.
3+
2. Set is Immutable object with duplicates not allowed, searchable
4+
*/
5+
6+
object Sets extends App
7+
{
8+
val set = Set.apply(1,2,3,4)
9+
val set2 = Set(1,2,3,4,5) //You can not create a set using new because trait Set is abstract; cannot be instantiated
10+
val set3 = Set(1,2,4,3,4,5)
11+
val set4 = Set(1,2,3,4,5,6,6,7)
12+
val set5 = Set(1,2)
13+
14+
println("set: " + set)
15+
println("set2: " + set2)
16+
println("set3: " + set3)
17+
println("set4: " + set4)
18+
19+
//Set Operations
20+
println("set diff set4 : " + set.diff(set4)) //empty set
21+
println("set4.diff(set) :" + set4.diff(set)) //Set(5,6,7)
22+
println("set union set3 :" + set.union(set3)) //Set(1,2,3,4,5)
23+
println("set intersect set3 : " + set.intersect(set3)) //Set(1,2,3,4)
24+
println("set5 intersect set4 : " + set5.intersect(set4))//Set(1,2)
25+
26+
//Set Union using ++
27+
println("set ++ set2 : " + (set ++ set2)) //Set(1,2,3,4,5)
28+
29+
//Adding elements from another List to Set
30+
println("set ++ List(15,19,20) : " + (set ++ List(15,19,20))) //Set(1,2,3,4,15,19,20)
31+
32+
println("set -- set5 : " + (set -- set5)) //Set subtraction
33+
println("set - 3 : " + (set - 3)) //remove element 3 from set
34+
35+
//Check if an element is present in set
36+
println("set.apply(4) : " + set.apply(4))
37+
println("set.apply(10) : " + set.apply(10)) //apply method is mapped to contains method in set
38+
println("set.contains(10) : " + set.contains(10))
39+
}
40+
41+
/**
42+
Sample Output
43+
set: Set(1, 2, 3, 4)
44+
set2: Set(5, 1, 2, 3, 4)
45+
set3: Set(5, 1, 2, 3, 4)
46+
set4: Set(5, 1, 6, 2, 7, 3, 4)
47+
set diff set4 : Set()
48+
set4.diff(set) :Set(5, 6, 7)
49+
set union set3 :Set(5, 1, 2, 3, 4)
50+
set intersect set3 : Set(1, 2, 3, 4)
51+
set5 intersect set4 : Set(1, 2)
52+
set ++ set2 : Set(5, 1, 2, 3, 4)
53+
set ++ List(15,19,20) : Set(20, 1, 2, 3, 19, 4, 15)
54+
set -- set5 : Set(3, 4)
55+
set - 3 : Set(1, 2, 4)
56+
set.apply(4) : true
57+
set.apply(10) : false
58+
set.contains(10) : false
59+
60+
**/

0 commit comments

Comments
 (0)