@@ -10,9 +10,7 @@ next-page: collections-methods
10
10
11
11
12
12
{% comment %}
13
- TODO: mention Array, ArrayDeque, ListBuffer, Queue, Stack, StringBuilder?
14
- LATER: note that methods like ` + ` , ` ++ ` , etc., are aliases for other methods
15
- LATER: add links to the Scaladoc for the major types shown here
13
+ TODO: note that methods like ` + ` , ` ++ ` , etc., are aliases for other methods
16
14
{% endcomment %}
17
15
18
16
@@ -119,7 +117,7 @@ The next several sections briefly demonstrate the `List`, `Vector`, and `ArrayBu
119
117
120
118
## ` List `
121
119
122
- [ The List type ] ( https://www.scala-lang.org/api/current/scala/collection/immutable/List.html ) is a linear, immutable sequence.
120
+ The [ List] ( https://www.scala-lang.org/api/current/scala/collection/immutable/List.html ) type is a linear, immutable sequence.
123
121
This just means that it’s a linked-list that you can’t modify.
124
122
Any time you want to add or remove ` List ` elements, you create a new ` List ` from an existing ` List ` .
125
123
488
486
489
487
## ArrayBuffer
490
488
491
- Use ` ArrayBuffer ` when you need a general-purpose, mutable indexed sequence in your Scala applications.
489
+ Use [ ArrayBuffer] ( https://www.scala-lang.org/api/current/scala/collection/mutable/ArrayBuffer.html ) when you need
490
+ a general-purpose, mutable indexed sequence in your Scala applications.
492
491
It’s mutable, so you can change its elements, and also resize it.
493
492
Because it’s indexed, random access of elements is fast.
494
493
@@ -603,7 +602,85 @@ a.update(0, 10) // ArrayBuffer(10, 2, 50, 4)
603
602
604
603
{% endtabs %}
605
604
605
+ ## Array
606
606
607
+ An [ Array] ( https://www.scala-lang.org/api/current/scala/Array.html ) is a mutable and indexed collection representing
608
+ Java arrays in Scala. It supports ` IndexedSeq ` operations due to having a ` scala.collection.mutable.ArraySeq `
609
+ implicit conversion, but isn't a part of the collections hierarchy.
610
+ Arrays allow for efficient access and updates of their elements, but cannot be resized.
611
+
612
+ ### Creating an Array
613
+
614
+ Create an ` Array ` by specifying its initial elements:
615
+
616
+ {% tabs array-init %}
617
+
618
+ {% tab 'Scala 2 and 3' %}
619
+ ``` scala
620
+ val a = Array (1 , 2 , 3 , 4 )
621
+ ```
622
+ {% endtab %}
623
+
624
+ {% endtabs %}
625
+
626
+ You can create an array of the required size without specifying its elements:
627
+
628
+ {% tabs array-create %}
629
+
630
+ {% tab 'Scala 2 and 3' %}
631
+ ``` scala
632
+ val a = Array .ofDim[Int ](10 )
633
+ ```
634
+ {% endtab %}
635
+
636
+ {% endtabs %}
637
+
638
+ The array elements will be set to either the corresponding Java primitive type default value or to ` null ` .
639
+
640
+ ### Updating Array elements
641
+
642
+ Update elements in an ` Array ` by either reassigning the desired element, or use the ` update ` method:
643
+
644
+ {% tabs array-update %}
645
+
646
+ {% tab 'Scala 2 and 3' %}
647
+ ``` scala
648
+ val a = Array (1 , 2 , 3 , 4 )
649
+ a(0 ) = 10 // Array(10, 2, 3, 4)
650
+ a.update(1 , 20 ) // Array(10, 20, 3, 4)
651
+ ```
652
+ {% endtab %}
653
+
654
+ {% endtabs %}
655
+
656
+ ### Adding elements to an Array
657
+
658
+ Arrays cannot be resized. Adding an element creates a new array and copies the previous elements into it.
659
+
660
+ {% tabs array-append %}
661
+
662
+ {% tab 'Scala 2 and 3' %}
663
+ ``` scala
664
+ val a = Array (1 , 2 , 3 , 4 )
665
+ val b = a :+ 5 // Array(1, 2, 3, 4, 5)
666
+ val c = b :++ Array (6 , 7 ) // Array(1, 2, 3, 4, 5, 6, 7)
667
+ val d = 0 +: c // Array(0, 1, 2, 3, 4, 5, 6, 7)
668
+ val e = Array (- 2 , - 1 ) ++: d // Array(-2, -1, 0, 1, 2, 3, 4, 5, 6, 7)
669
+ ```
670
+ {% endtab %}
671
+
672
+ {% endtabs %}
673
+
674
+ ### Differences between Array and ArrayBuffer
675
+
676
+ Despite having similar names, there are important differences between the two types:
677
+
678
+ - ` ArrayBuffer ` is backed by an ` Array ` that gets substituted by a larger one when needed. Appending to an
679
+ ` ArrayBuffer ` takes amortized constant time.
680
+ - ` Array ` is represented by a Java array and cannot be resized. Appending to an ` Array ` creates a new array and takes linear time.
681
+ - The array backing an ` ArrayBuffer ` instance has the ` Array[AnyRef] ` type, corresponding to the ` Object[] ` Java array.
682
+ ` Array ` of a value type, e.g., ` Array[Int] ` is represented by a Java array of a primitive type, e.g., ` int[] ` . When
683
+ using arrays of value types, autoboxing can be avoided, which translates into higher performance.
607
684
608
685
## Maps
609
686
@@ -959,6 +1036,153 @@ val map = (1 to 3).map(e => (e,s"$e")).toMap
959
1036
{% endtabs %}
960
1037
961
1038
1039
+ ## Other collections
1040
+
1041
+ While the collections listed above are enough to cover most use cases, there are several more specialized types:
1042
+
1043
+ ### StringBuilder
1044
+
1045
+ A [ StringBuilder] ( https://www.scala-lang.org/api/current/scala/collection/mutable/StringBuilder.html ) is a mutable,
1046
+ indexed sequence used to build strings by concatenating their fragments.
1047
+
1048
+ Create a ` StringBuilder ` , optionally specifying its initial capacity and content.
1049
+
1050
+ {% tabs stringbuilder-create %}
1051
+
1052
+ {% tab 'Scala 2 and 3' %}
1053
+ ``` scala
1054
+ import scala .collection .mutable .StringBuilder
1055
+ val a = new StringBuilder (4 , " ab" )
1056
+ ```
1057
+ {% endtab %}
1058
+
1059
+ {% endtabs %}
1060
+
1061
+ The ` StringBuilder ` will have free space to accommodate four characters in addition to the space taken by its initial
1062
+ ` "ab" ` value.
1063
+
1064
+ Append strings and characters to build the result. ` StringBuilder ` allows for updating characters in place.
1065
+
1066
+ {% tabs stringbuilder-use %}
1067
+
1068
+ {% tab 'Scala 2 and 3' %}
1069
+ ``` scala
1070
+ a ++= " cde"
1071
+ a += 'f'
1072
+ a(0 ) = 'A'
1073
+ ```
1074
+ {% endtab %}
1075
+
1076
+ {% endtabs %}
1077
+
1078
+ Use the current result with the ` result() ` method and clear the builder with ` clear() ` .
1079
+
1080
+ {% tabs stringbuilder-result %}
1081
+
1082
+ {% tab 'Scala 2 and 3' %}
1083
+ ``` scala
1084
+ val b = a.result() // b: "Abcdef"
1085
+ a.clear()
1086
+ val c = a.result() // c: ""
1087
+ ```
1088
+ {% endtab %}
1089
+
1090
+ {% endtabs %}
1091
+
1092
+ ### ListBuffer
1093
+
1094
+ A [ ListBuffer] ( https://www.scala-lang.org/api/current/scala/collection/mutable/ListBuffer.html ) is a mutable, linear
1095
+ sequence similar to ` ArrayBuffer ` , but backed by a list. It provides constant time append
1096
+ and prepend operations, while all other operations take linear time.
1097
+
1098
+ Create either an empty ` ListBuffer ` or with initial elements.
1099
+
1100
+ {% tabs listbuffer-create %}
1101
+
1102
+ {% tab 'Scala 2 and 3' %}
1103
+ ``` scala
1104
+ import collection .mutable .ListBuffer
1105
+
1106
+ val a = ListBuffer (20 , 30 , 40 )
1107
+ val b = ListBuffer .empty[Int ]
1108
+ ```
1109
+ {% endtab %}
1110
+
1111
+ {% endtabs %}
1112
+
1113
+ Append and prepend elements.
1114
+
1115
+ {% tabs listbuffer-use %}
1116
+
1117
+ {% tab 'Scala 2 and 3' %}
1118
+ ``` scala
1119
+ a += 50 // ListBuffer(20, 30, 40, 50)
1120
+ a ++= Seq (60 , 70 ) // ListBuffer(20, 30, 40, 50, 60)
1121
+ 10 +=: a // ListBuffer(10, 20, 30, 40, 50, 60)
1122
+ ```
1123
+ {% endtab %}
1124
+
1125
+ {% endtabs %}
1126
+
1127
+ Convert to an immutable list when it's ready.
1128
+
1129
+ {% tabs listbuffer-convert %}
1130
+
1131
+ {% tab 'Scala 2 and 3' %}
1132
+ ``` scala
1133
+ a.toList // List(10, 20, 30, 40, 50, 60)
1134
+ ```
1135
+ {% endtab %}
1136
+
1137
+ {% endtabs %}
1138
+
1139
+ ### Stack
1140
+
1141
+ A [ Stack] ( https://www.scala-lang.org/api/current/scala/collection/mutable/Stack.html ) is a mutable, last-in-first-out
1142
+ collection that allows to efficiently push (prepend) and pop (remove the last) elements.
1143
+
1144
+ Create a ` Stack ` :
1145
+
1146
+ {% tabs stack-create %}
1147
+
1148
+ {% tab 'Scala 2 and 3' %}
1149
+ ``` scala
1150
+ import collection .mutable .Stack
1151
+
1152
+ val a = Stack (1 , 2 )
1153
+ val b = Stack ()
1154
+ ```
1155
+ {% endtab %}
1156
+
1157
+ {% endtabs %}
1158
+
1159
+ Push and pop elements from a ` Stack ` :
1160
+
1161
+ {% tabs stack-use %}
1162
+
1163
+ {% tab 'Scala 2 and 3' %}
1164
+ ``` scala
1165
+ a.push(0 ) // Stack(0, 1, 2)
1166
+ val elem = a.pop // elem: 2
1167
+ a // Stack(0, 1)
1168
+ ```
1169
+ {% endtab %}
1170
+
1171
+ {% endtabs %}
1172
+
1173
+ ### Queue
1174
+
1175
+ A ` Queue ` is a first-in-first-out collection that allows to efficiently enqueue (prepend) and dequeue (remove the first)
1176
+ elements. Scala has both [ mutable] ( https://www.scala-lang.org/api/current/scala/collection/mutable/Queue.html )
1177
+ and [ immutable] ( https://www.scala-lang.org/api/current/scala/collection/immutable/Queue.html ) queue types.
1178
+
1179
+ ### ArrayDeque
1180
+
1181
+ An [ ArrayDeque] ( https://www.scala-lang.org/api/current/scala/collection/mutable/ArrayDeque.html ) is a double-ended queue
1182
+ that supports amortized constant time ` append ` , ` prepend ` , ` removeHead ` and ` removeLast ` operations. It's a mutable,
1183
+ indexed collection and an alternative for ` ArrayBuffer ` when prepends are needed.
1184
+
1185
+
962
1186
## More details
963
1187
964
1188
When you need more information about specialized collections, see the following resources:
0 commit comments