Creating Collections From Scratch
Outdated Notice
You have syntaxList(1, 2, 3)
to create a list of three integers andMap('A' -> 1, 'C' -> 2)
to create a map with two bindings. This is actually a universal feature of Scala collections. You can take any collection name and follow it by a list of elements in parentheses. The result will be a new collection with the given elements. Here are some more examples:
Traversable() // An empty traversable objectList() // The empty listList(1.0, 2.0) // A list with elements 1.0, 2.0Vector(1.0, 2.0) // A vector with elements 1.0, 2.0Iterator(1, 2, 3) // An iterator returning three integers.Set(dog, cat, bird) // A set of three animalsHashSet(dog, cat, bird) // A hash set of the same animalsMap('a' -> 7, 'b' -> 0) // A map from characters to integers
“Under the covers” each of the above lines is a call to theapply
method of some object. For instance, the third line above expands to
List.apply(1.0, 2.0)
So this is a call to theapply
method of the companion object of theList
class. That method takes an arbitrary number of arguments and constructs a list from them. Every collection class in the Scala library has a companion object with such anapply
method. It does not matter whether the collection class represents a concrete implementation, likeList
, orStream
orVector
, do, or whether it is an abstract base class such asSeq
,Set
orTraversable
. In the latter case, calling apply will produce some default implementation of the abstract base class. Examples:
scala> List(1, 2, 3)res17: List[Int] = List(1, 2, 3)scala> Traversable(1, 2, 3)res18: Traversable[Int] = List(1, 2, 3)scala> mutable.Traversable(1, 2, 3)res19: scala.collection.mutable.Traversable[Int] = ArrayBuffer(1, 2, 3)
Besidesapply
, every collection companion object also defines a memberempty
, which returns an empty collection. So instead ofList()
you could writeList.empty
, instead ofMap()
,Map.empty
, and so on.
Descendants ofSeq
classes provide also other factory operations in their companion objects. These are summarized in the following table. In short, there’s
concat
, which concatenates an arbitrary number of traversables together,fill
andtabulate
, which generate single or multidimensional sequences of given dimensions initialized by some expression or tabulating function,range
, which generates integer sequences with some constant step length, anditerate
, which generates the sequence resulting from repeated application of a function to a start element.
Factory Methods for Sequences
WHAT IT IS | WHAT IT DOES |
---|---|
S.empty | The empty sequence. |
S(x, y, z) | A sequence consisting of elementsx, y, z . |
S.concat(xs, ys, zs) | The sequence obtained by concatenating the elements ofxs, ys, zs . |
S.fill(n){e} | A sequence of lengthn where each element is computed by expressione . |
S.fill(m, n){e} | A sequence of sequences of dimensionm×n where each element is computed by expressione . (exists also in higher dimensions). |
S.tabulate(n){f} | A sequence of lengthn where the element at each index i is computed byf(i) . |
S.tabulate(m, n){f} | A sequence of sequences of dimensionm×n where the element at each index(i, j) is computed byf(i, j) . (exists also in higher dimensions). |
S.range(start, end) | The sequence of integersstart …end-1 . |
S.range(start, end, step) | The sequence of integers starting withstart and progressing bystep increments up to, and excluding, theend value. |
S.iterate(x, n)(f) | The sequence of lengthn with elementsx ,f(x) ,f(f(x)) , … |
Contributors to this page:
Contents
- Introduction
- Mutable and Immutable Collections
- Trait Traversable
- Trait Iterable
- The sequence traits Seq, IndexedSeq, and LinearSeq
- Sets
- Maps
- Concrete Immutable Collection Classes
- Concrete Mutable Collection Classes
- Arrays
- Strings
- Performance Characteristics
- Equality
- Views
- Iterators
- Creating Collections From Scratch
- Conversions Between Java and Scala Collections
- Migrating from Scala 2.7