-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMapDemo_2.scala
52 lines (35 loc) · 944 Bytes
/
MapDemo_2.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package ScalaBasic
// https://www.youtube.com/watch?v=jUlvvtlJOSI&list=PLmOn9nNkQxJEqCNXBu5ozT_26xwvUbHyE&index=128
object MapDemo_2 extends App {
/**
* Scala immutable map
* 1) is ordering
* 2) underlying data structure is : Tuple2
* 3) by default, map in Scala is immutable map
*/
val map2 = Map("jack" -> 10, "tim" -> 0, "amy" -> 999)
println("map2 = " + map2)
println("=================")
// get element from map
println(map2("jack"))
println("=================")
// get keys from map
println(map2.keys)
println("=================")
// get values from map
println(map2.values)
println("=================")
// loop over map
for (i <- map2){
println(i)
}
println("=================")
// loop over map : (k,v)
for ((k, v) <- map2) {
println((k,v))
}
println("=================")
for (i <- map2) {
println("key = " + i._1 + ", value = " + i._2)
}
}