An Overview of the Collections API | Contents |
Traversable
Iterable
Seq
IndexedSeq
immutable.Vector
mutable.ResizableArray
mutable.ArraySeq
mutable.StringBuilder
(String, Array)
LinearSeq
immutable.List
immutable.Stream
mutable.MutableList
mutable.Queue
mutable.Buffer
mutable.ListBuffer
mutable.ArrayBuffer
Range
Set
SortedSet
immutable.TreeSet
immutable.HashSet
mutable.HashSet
mutable.LinkedHashSet
BitSet
(immutable.EmptySet, immutable.Set1, ..., immutable.Set4)
Map
SortedMap
immutable.TreeMap
immutable.HashMap
mutable.HashMap
mutable.LinkedHashMap
(immutable.EmptyMap, immutable.Map1, ..., immutable.Map4)
The most important collection classes are shown in the figure above. There is quite a bit of commonality shared by all these classes. For instance, every kind of collection can be created by the same uniform syntax, writing the collection class name followed by its elements:
Traversable(1, 2, 3)
Iterable("x", "y", "z")
Map("x" -> 24, "y" -> 25, "z" -> 26)
Set(Color.red, Color.green, Color.blue)
SortedSet("hello", "world")
Buffer(x, y, z)
IndexedSeq(1.0, 2.0)
LinearSeq(a, b, c)
List(1, 2, 3)
HashMap("x" -> 24, "y" -> 25, "z" -> 26)
All collections support the API provided by Traversable, but specialize types wherever this makes sense. For instance the map method in class Traversable returns another Traversable as its result. But this result type is overridden in subclasses. For instance, calling map on a List yields again a List, calling it on a Set yields again a Set and so on.
scala> List(1, 2, 3) map (_ + 1)
res0: List[Int] = List(2, 3, 4)
scala> Set(1, 2, 3) map (_ * 2)
res0: Set[Int] = Set(2, 4, 6)
Most of the classes in the collections hierarchy exist in three variants: root, mutable, and immutable. The only exception is the Buffer trait which only exists as a mutable collection.
In the following, we will review these classes one by one.
Next: Trait Traversable
An Overview of the Collections API | Contents |