-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scala
executable file
·906 lines (638 loc) · 19 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
****** Scala ******
Scala is a compiler based programming language
Build on funtional programing principles
Compiles into byte code
required jdk and jre
Seamless integeration with java
REPL - Read ,Evaluate ,print and loop
println("Hello world")
First it reads the code,then evaluate which compile and produces byte code,print the output and loop which means it is waiting for next set of instructions
:quit
:help
ctrl+L will clear the screens
:history for history of the commands
:history 10
:save <path> to save the commands
:h? sc -> to search a keyword in history
**** Basic Programming constructs ****
Instead of giving each line in command line and executing ,we can execute a complete block of code.
Use :paste
and to run the block , ctrl+d
The disadvantage of using command line than paste mode is,
each line will be treated as a seperate program.e.,g
scala> val i = 10
i: Int = 10
scala> val i = "raj"
i: String = raj
In the above case,it allows i to be overwritten.
But in :paste mode,it wont allow us to overwwritte the value
--------------------
scala> :paste
// Entering paste mode (ctrl-D to finish)
val i = 10
val i ="raj"
// Exiting paste mode, now interpreting.
<console>:8: error: i is already defined as value i
val i ="raj"
^
--------------------
val : It is immutable...cannot be changed
var : mutable...it can be changed
e.g.,
val x: Int = 10
val t: String ="Raj"
val f: Float=10;
val b:Boolean = true;
The above values cannot be modified.
var t:Int = 12
var bool:Boolean=false
The above values can be modified
Type inference but not Dynamic typed
var d = true;
var s = "Raj";
We cannot change the type once it is assigned.You will get type mismatch error.e.g.,
-------------------------
scala> :paste
// Entering paste mode (ctrl-D to finish)
var i = 1;
i = "Raj"
// Exiting paste mode, now interpreting.
<console>:9: error: type mismatch;
found : String("Raj")
required: Int
i = "Raj"
^
-------------------------
In scala,all arithmetich operations are functions. + , -, / ,*
scala> val i = 1 + 2
i: Int = 3
scala> val i = 1.+(2)
i: Int = 3
we can call the function as .+() . It is internally executed like this only
**** to get the list of functions supported for variable say i, we can type i.<tab> .It will list all functions.
------------------------------------
scala> i.
% + > >>> isInstanceOf toDouble toLong unary_+ |
& - >= ^ toByte toFloat toShort unary_-
* / >> asInstanceOf toChar toInt toString unary_~
scala> val s= "Raj"
s: String = Raj
scala> s.
+ concat isInstanceOf startsWith
asInstanceOf contains lastIndexOf subSequence
charAt contentEquals length substring
chars endsWith matches toCharArray
codePointAt equalsIgnoreCase offsetByCodePoints toLowerCase
codePointBefore getBytes regionMatches toString
codePointCount getChars replace toUpperCase
codePoints indexOf replaceAll trim
compareTo intern replaceFirst
compareToIgnoreCase isEmpty split
scala> s.
+ codePointBefore concat getBytes isInstanceOf regionMatches startsWith toString
asInstanceOf codePointCount contains getChars lastIndexOf replace subSequence toUpperCase
charAt codePoints contentEquals indexOf length replaceAll substring trim
chars compareTo endsWith intern matches replaceFirst toCharArray
codePointAt compareToIgnoreCase equalsIgnoreCase isEmpty offsetByCodePoints split toLowerCase
scala> s.to
toCharArray toLowerCase toString toUpperCase
scala> s.toUpperCase
res6: String = RAJ
-------------------------------------------
**** if you do not know the argument of any function you can click tab twice .It will list the function definitiion.
e.g., s.lastIndexOf<tab><tab>
-----------------------
scala> s.lastIndexOf
def lastIndexOf(Int): Int def lastIndexOf(Int, Int): Int def lastIndexOf(String): Int def lastIndexOf(String, Int): Int
-----------------------
****Collection
To create a collection in scala,we can just type (1 to 100).
scala> 1 to 100
res7: scala.collection.immutable.Range.Inclusive = Range(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, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)
This has create a collection with res7 as reference.
To iterate over this collection, using for loop, then
scala> for (i <- 1 to 100) {
| print(i)
| }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
OR
scala> for (i <- 1 to 100) {
| println(i)
| }
OR
scala> for (i <- res7){
| print(i)
| }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
OR
scala> res7.foreach(print)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
******* To get sum of all numbers using for loop
This is conventional approach
scala> :paste
// Entering paste mode (ctrl-D to finish)
var sum = 0;
for(element <- 1 to 100){
sum =sum+element;
}
// Exiting paste mode, now interpreting.
sum: Int = 5050
****** to get sum of even number
scala> :paste
// Entering paste mode (ctrl-D to finish)
var sum = 0;
for(element <- 1 to 100){
if(element % 2 == 0){
sum = sum+element;
}
}
// Exiting paste mode, now interpreting.
sum: Int = 2550
Block of expression
With Semi colon (To write in a single line)
scala> val x = {
| val a = 100;
| val b = 200;
| a + b
| }
x: Int = 300
Without semi colon (To write in multiple lines)
scala> val x = {
| val a =1.3
| val b =2.3
| a * b
| }
x: Double = 2.9899999999999998
Lazy Evaluation or on demand loading
scala> val x = 12;
x: Int = 12
scala> x * 10
res5: Int = 120
The above code allocates the memory and assign 12 to x.This is an issue because,This variable might be used in the program or not.
scala> lazy val x =100;
x: Int = <lazy>
The above code does not allocates the memory.This is an issue because,The memory will be allocated during computation
scala> x * 2
res6: Int = 200
Another example
scala> val x = (1 to 1600000).toList
x: List[Int] = List(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, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, ...
scala> x.reduce(_ + _)
res7: Int = 100545792
scala> lazy val x = (1 to 1600000).toList
x: List[Int] = <lazy>
scala> x.reduce(_ + _)
res8: Int = 100545792
Lazy values are useful for delaying costly intizializtion instructions..
Loop###
scala> val str = "Test"
str: String = Test
scala> str.foreach(println)
T
e
s
t
scala> str.foreach(print)
Test
-- For each takes only a function
For Loop
syntax : for(arg <- collection) code.....
scala> for (i <- 1 to 5) println(i)
1
2
3
4
5
The above prints 1 to 5
scala> for (i <- 5 to 1) println(i)
The above code does not print because,by default increemnt is one and If you want to print from 5 to 1..Do it as below,
scala> for (i <- 5 to 1 by -1) println(i)
5
4
3
2
1
More example,
scala> for (i <- 1 to 5) println(i)
1
2
3
4
5
scala> for (i <- 5 to 1) println(i)
scala> for (i <- 5 to 1 by -1) println(i)
5
4
3
2
1
scala> for (i <- 5 to 1 by -2) println(i)
5
3
1
scala> for (i <- 5 to 1 by -3) println(i)
5
2
scala> for (i <- 5 to 1 by 1) println(i)
scala> for (i <- 5 to 1 by -1) println(i)
5
4
3
2
1
scala> for (i <- 5 to 1 by -2) println(i)
5
3
1
scala> for (i <- 1 to 5 by 1) println(i)
1
2
3
4
5
scala> for (i <- 1 to 5 by 2) println(i)
1
3
5
scala> for(i <- 1 to 5; j <- 1 to 4) println(s"($i,$j)")
(1,1)
(1,2)
(1,3)
(1,4)
(2,1)
(2,2)
(2,3)
(2,4)
(3,1)
(3,2)
(3,3)
(3,4)
(4,1)
(4,2)
(4,3)
(4,4)
(5,1)
(5,2)
(5,3)
(5,4)
In the above code, $ is the substitue for i and j..which scala uses it.
scala> for ( i <- 1 to 5; j <- 1 to 4)
| {
| if(i == j)
| println(s"($i,$j)")
| }
(1,1)
(2,2)
(3,3)
(4,4)
The below code is more efficient than above -> Gaurd function
scala> for (i <- 1 to 5; j <- 1 to 4 if i==j)
| println(s"($i,$j)")
(1,1)
(2,2)
(3,3)
(4,4)
Yield
The result of each loop of iteration is stored in a List(Vector)
scala> for(i <- 1 to 20 if i % 2 == 0) yield(i)
res15: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
scala> val x = for(i <- 1 to 20 if i % 2 == 0) yield(i)
x: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
scala> x
res16: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
scala> x.foreach(print)
2468101214161820
scala> x.foreach(println)
2
4
6
8
10
12
14
16
18
20
function
syntax
def functioname(function param): returnType = {
code....
}
def areaRect(l : Float.b :Float): Float = {
l *b
}
scala> areaRect(2.3f,6.7f)
res22: Float = 15.409999
if you do not want return anything,then mention the return type as Unit
(like void in java)
scala> def areaRect(l:Float,b:Float):Unit = {
| println(l)
| }
areaRect: (l: Float, b: Float)Unit
*** Return type in the function not mandatory.Because the scala will either return the data or unit based on your pogram.It is intelligent enough to identify whether you function return or not
scala> areaRect(2.3f,6.7f)
2.3
scala> def isEvenNumber(number:Int):Boolean = {
| number % 2 == 0
| }
isEvenNumber: (number: Int)Boolean
scala> isEvenNumber(3)
res24: Boolean = false
scala> isEvenNumber(30)
res25: Boolean = true
scala> def isEvenNumber(number:Int):Boolean = {
| if(number % 2 == 0 ) true
| else false
| }
isEvenNumber: (number: Int)Boolean
scala> isEvenNumber(30)
res26: Boolean = true
Default values can be set in the parameters of the function, as below.
scala> def areaRect(l:Int,b:Int):Int = { l*b }
areaRect: (l: Int, b: Int)Int
scala> areaRect(2,3)
res27: Int = 6
scala> def areaRect(l:Int,b:Int=10):Int = { l*b }
areaRect: (l: Int, b: Int)Int
scala> areaRect(2,3)
res28: Int = 6
scala> areaRect(2)
res29: Int = 20
scala> def areaRect(l:Int=12,b:Int):Int = { l*b }
areaRect: (l: Int, b: Int)Int
scala> areaRect(2,3)
res30: Int = 6
scala> areaRect(2)
<console>:9: error: not enough arguments for method areaRect: (l: Int, b: Int)Int.
Unspecified value parameter b.
areaRect(2)
^
scala> areaRect(b = 2)
res32: Int = 24
********Function passing function
Defining return type is optional
---------------------
scala> :paste
// Entering paste mode (ctrl-D to finish)
def sum(func: Int => Int,lb :Int,ub:Int) = {
var total = 0;
for(element <- lb to ub){
total = total + func(element)
}
total
}
// Exiting paste mode, now interpreting.
sum: (func: Int => Int, lb: Int, ub: Int)Int
scala> def id(i:Int) = i
id: (i: Int)Int
scala> def sqr(i:Int) = i*i
sqr: (i: Int)Int
scala> def cube(i:Int) = i*i*i
cube: (i: Int)Int
scala> def multOf100(i:Int) = i*100
multOf100: (i: Int)Int
scala> sum(id,1,10)
res0: Int = 55
scala> sum(sqr,1,10)
res1: Int = 385
scala> sum(cube,1,10)
res2: Int = 3025
scala> sum(multof100,1,10)
<console>:9: error: not found: value multof100
sum(multof100,1,10)
^
scala> sum(multOf100,1,10)
res4: Int = 5500
****** Anonymous functions **********
Anonymous functions
can be assigned to variables
can be passed as arguments to other functions
Anonymous function doesnt have any name.
We can pass as arguments to the othe function
e.g.,
--------------
i is the argument name = > is is the return type
scala> sum(i => i,1,10)
res5: Int = 55
scala> sum(i => i*i,1,10)
res6: Int = 385
scala> sum(i => i*i*i,1,10)
res7: Int = 3025
scala> sum(i => i*100,1,10)
res8: Int = 5500
scala> sum (i => {
| if(i % 2 ==0) {
| i*i
| } else {
| i * i * i
| }
| } , 1,10)
Data type is very important
scala> sum (i => {
| if(i % 2 ==0) {
| i*i
| } else {
| "Raj"
| }
| } , 1,10)
<console>:13: error: type mismatch;
found : String("Raj")
required: Int
"Raj"
^
-----------------
-------------------
Collections
Array
scala> val arr = new Array[Int](10)
arr: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala> arr(0) = 1
scala> arr(1) = 2
scala> arr(10) = 3
java.lang.ArrayIndexOutOfBoundsException: 10
... 33 elided
scala> arr(9) = 3
scala> arr
res41: Array[Int] = Array(1, 2, 0, 0, 0, 0, 0, 0, 0, 3)
scala> arr.foreach(println)
1
2
0
0
0
0
0
0
0
3
scala> val arr = new Array[String](10)
arr: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)
scala> arr(0)
res43: String = null
scala> arr(0) = "Raj"
scala> arr(2) = "sekar"
scala> arr.foreach(println)
Raj
null
sekar
null
null
null
null
null
null
null
scala> val arr = Array(1,2,3,4)
arr: Array[Int] = Array(1, 2, 3, 4)
scala> arr.foreach(println)
1
2
3
4
//if an Object is declared as val,only the object reference becomes immutable and not the object value.hence you can change the value of the object.
e.g.,
scala> val arr = Array(1,2,3,4)
arr: Array[Int] = Array(1, 2, 3, 4)
scala> val arr1 = Array(1,2,3,4)
arr1: Array[Int] = Array(1, 2, 3, 4)
scala> arr = arr1
<console>:9: error: reassignment to val
arr = arr1
^
scala> var arr = Array(1,2,3,4)
arr: Array[Int] = Array(1, 2, 3, 4)
scala> var arr1 = Array(1,2,3,4)
arr1: Array[Int] = Array(1, 2, 3, 4)
scala> arr = arr1
arr: Array[Int] = [I@720cbb7d
ArrayBuffer
scala> val arr = new ArrayBuffer[Int]()
<console>:7: error: not found: type ArrayBuffer
val arr = new ArrayBuffer[Int]()
^
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
scala> val arr = new ArrayBuffer[Int]()
arr: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> arr + = 200
<console>:12: error: missing arguments for method + in class any2stringadd;
follow this method with `_' if you want to treat it as a partially applied function
val $ires0 = arr.$plus
^
<console>:9: error: reassignment to val
arr + = 200
^
scala> arr += 200
res0: arr.type = ArrayBuffer(200)
scala> arr += 300
res1: arr.type = ArrayBuffer(200, 300)
scala> arr += (100,400,600)
res2: arr.type = ArrayBuffer(200, 300, 100, 400, 600)
scala> arr.foreach(arr)
java.lang.IndexOutOfBoundsException: 200
at scala.collection.mutable.ResizableArray$class.apply(ResizableArray.scala:43)
at scala.collection.mutable.ArrayBuffer.apply(ArrayBuffer.scala:48)
at scala.collection.mutable.ArrayBuffer.apply(ArrayBuffer.scala:48)
at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
... 33 elided
scala> arr.foreach(println)
200
300
100
400
600
scala> arr += Array(1000,4000,6000)
<console>:10: error: type mismatch;
found : Array[Int]
required: Int
arr += Array(1000,4000,6000)
^
scala> arr ++= Array(1000,4000,6000)
res6: arr.type = ArrayBuffer(200, 300, 100, 400, 600, 1000, 4000, 6000)
scala> arr --= Array(1000,4000,6000)
res7: arr.type = ArrayBuffer(200, 300, 100, 400, 600)
scala> for(n <- arr) println(n)
200
300
100
400
600
scala> arr.foreach(println)
200
300
100
400
600
scala> for(n <- arr if n % 2 != 0) println(n)
scala> for(n <- arr if n % 2 = 0) println(n)
<console>:1: error: ')' expected but '=' found.
for(n <- arr if n % 2 = 0) println(n)
^
scala> for(n <- arr if n % 2 == 0) println(n)
200
300
100
400
600
scala> for(n <- arr if n % 2 == 0) yield(n)
res12: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(200, 300, 100, 400, 600)
scala> arr.filter(_ % 2 != 0).foreach(println)
scala> arr.filter(_ % 2 == 0).foreach(println)
200
300
100
400
600
scala> arr.max
res15: Int = 600
scala> arr.min
res16: Int = 100
scala> arr.max
res17: Int = 600
Maps
The below is the immutable map.Once created you cannot delete the key.
scala> val m = Map(1 -> "Binny", 2 -> "Stuart")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> Binny, 2 -> Stuart)
scala> m -= 1
<console>:10: error: value -= is not a member of scala.collection.immutable.Map[Int,String]
m -= 1
^
So,You need to create a mutable map
scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map
scala> val m = Map(1 -> "Binny", 2 -> "Stuart")
m: scala.collection.mutable.Map[Int,String] = Map(2 -> Stuart, 1 -> Binny)
scala> m -= 1
res19: m.type = Map(2 -> Stuart)
scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map
scala> val m = Map(1 -> "Binny", 2 -> "Stuart")
m: scala.collection.mutable.Map[Int,String] = Map(2 -> Stuart, 1 -> Binny)
scala> m -= 1
res19: m.type = Map(2 -> Stuart)
scala> m
res20: scala.collection.mutable.Map[Int,String] = Map(2 -> Stuart)
scala> m(1)
java.util.NoSuchElementException: key not found: 1
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:59)
at scala.collection.mutable.HashMap.apply(HashMap.scala:65)
... 33 elided
scala> m(2)
res22: String = Stuart
scala> m.get(1)
res23: Option[String] = None
scala> m.get(2)
res24: Option[String] = Some(Stuart)
scala> m.getOrElse(1, "Raj")
res25: String = Raj
scala> m
res26: scala.collection.mutable.Map[Int,String] = Map(2 -> Stuart)
scala> m.getOrElse(10, 100)
res27: Any = 100
scala> m += (1 -> "Sekar")
res28: m.type = Map(2 -> Stuart, 1 -> Sekar)
scala> m
res29: scala.collection.mutable.Map[Int,String] = Map(2 -> Stuart, 1 -> Sekar)
mutable vs immutable collections