package immutable
- Alphabetic
- By Inheritance
- immutable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
case class
::[+A](x: A, next: List[A]) extends List[A] with Product with Serializable
- Annotations
- @SerialVersionUID()
-
sealed abstract
class
BitSet extends SortedSet[Int] with collection.BitSet with SortedSetOps[Int, SortedSet, BitSet] with BitSetOps[BitSet] with StrictOptimizedIterableOps[Int, Set, BitSet] with Serializable
A class for immutable bitsets.
A class for immutable bitsets.
Bitsets are sets of non-negative integers which are represented as variable-size arrays of bits packed into 64-bit words. The memory footprint of a bitset is determined by the largest number stored in it.
- Annotations
- @SerialVersionUID()
- See also
"Scala's Collection Library overview" section on
Immutable BitSets
for more information.
-
sealed abstract
class
HashMap[K, +V] extends Map[K, V] with MapOps[K, V, HashMap, HashMap[K, V]] with StrictOptimizedIterableOps[(K, V), Iterable, HashMap[K, V]] with Serializable
This class implements immutable maps using a hash trie.
This class implements immutable maps using a hash trie.
Note: The builder of this hash map may return specialized representations for small maps.
- K
the type of the keys contained in this hash map.
- V
the type of the values associated with the keys.
- Version
2.8
- Since
2.3
- See also
"Scala's Collection Library overview" section on
Hash Tries
for more information.
-
sealed abstract
class
HashSet[A] extends Set[A] with SetOps[A, HashSet, HashSet[A]] with StrictOptimizedIterableOps[A, HashSet, HashSet[A]] with Serializable
This class implements immutable sets using a hash trie.
This class implements immutable sets using a hash trie.
Note: The builder of this hash set may return specialized representations for small sets.
- A
the type of the elements contained in this hash set.
- Annotations
- @SerialVersionUID()
- Version
2.8
- Since
2.3
-
sealed abstract
class
ImmutableArray[+A] extends IndexedSeq[A] with IndexedSeqOps[A, ImmutableArray, ImmutableArray[A]] with StrictOptimizedSeqOps[A, ImmutableArray, ImmutableArray[A]]
An immutable array.
An immutable array.
Supports efficient indexed access and has a small memory footprint.
-
trait
IndexedSeq[+A] extends Seq[A] with collection.IndexedSeq[A] with IndexedSeqOps[A, IndexedSeq, IndexedSeq[A]]
Base trait for immutable indexed sequences that have efficient
apply
andlength
-
trait
IndexedSeqOps[+A, +CC[X] <: IndexedSeq[X], +C] extends SeqOps[A, CC, C] with collection.IndexedSeqOps[A, CC, C]
Base trait for immutable indexed Seq operations
-
sealed abstract
class
IntMap[+T] extends Map[Int, T] with MapOps[Int, T, Map, IntMap[T]] with StrictOptimizedIterableOps[(Int, T), Iterable, IntMap[T]]
Specialised immutable map structure for integer keys, based on Fast Mergeable Integer Maps by Okasaki and Gill.
Specialised immutable map structure for integer keys, based on Fast Mergeable Integer Maps by Okasaki and Gill. Essentially a trie based on binary digits of the integers.
Note: This class is as of 2.8 largely superseded by HashMap.
- T
type of the values associated with integer keys.
- Since
2.7
-
trait
Iterable[+A] extends collection.Iterable[A] with IterableOps[A, Iterable, Iterable[A]]
A trait for collections that are guaranteed immutable.
A trait for collections that are guaranteed immutable.
- A
the element type of the collection
-
sealed abstract
class
LazyList[+A] extends LinearSeq[A] with LinearSeqOps[A, LazyList, LazyList[A]]
The class
LazyList
implements lazy lists where elements are only evaluated when they are needed.The class
LazyList
implements lazy lists where elements are only evaluated when they are needed. Here is an example:import scala.math.BigInt object Main extends App { lazy val fibs: LazyList[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map { n => n._1 + n._2 } fibs take 5 foreach println } // prints // // 0 // 1 // 1 // 2 // 3
The
LazyList
class also employs memoization such that previously computed values are converted fromLazyList
elements to concrete values of typeA
. To illustrate, we will alter body of thefibs
value above and take some more values:import scala.math.BigInt object Main extends App { lazy val fibs: LazyList[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip( fibs.tail).map(n => { println("Adding %d and %d".format(n._1, n._2)) n._1 + n._2 }) fibs take 5 foreach println fibs take 6 foreach println } // prints // // 0 // 1 // Adding 0 and 1 // 1 // Adding 1 and 1 // 2 // Adding 1 and 2 // 3 // And then prints // // 0 // 1 // 1 // 2 // 3 // Adding 2 and 3 // 5
There are a number of subtle points to the above example.
- The definition of
fibs
is aval
not a method. The memoization of theLazyList
requires us to have somewhere to store the information and aval
allows us to do that. - While the
LazyList
is actually being modified during access, this does not change the notion of its immutability. Once the values are memoized they do not change and values that have yet to be memoized still "exist", they simply haven't been realized yet. - One must be cautious of memoization; you can very quickly eat up large
amounts of memory if you're not careful. The reason for this is that the
memoization of the
LazyList
creates a structure much like scala.collection.immutable.List. So long as something is holding on to the head, the head holds on to the tail, and so it continues recursively. If, on the other hand, there is nothing holding on to the head (e.g. we useddef
to define theLazyList
) then once it is no longer being used directly, it disappears. - Note that some operations, including drop, dropWhile,
flatMap or collect may process a large number of intermediate
elements before returning. These necessarily hold onto the head, since
they are methods on
LazyList
, and a lazy list holds its own head. For computations of this sort where memoization is not desired, useIterator
when possible.
// For example, let's build the natural numbers and do some silly iteration // over them. // We'll start with a silly iteration def loop(s: String, i: Int, iter: Iterator[Int]): Unit = { // Stop after 200,000 if (i < 200001) { if (i % 50000 == 0) println(s + i) loop(s, iter.next(), iter) } } // Our first LazyList definition will be a val definition val lazylist1: LazyList[Int] = { def loop(v: Int): LazyList[Int] = v #:: loop(v + 1) loop(0) } // Because lazylist1 is a val, everything that the iterator produces is held // by virtue of the fact that the head of the LazyList is held in lazylist1 val it1 = lazylist1.iterator() loop("Iterator1: ", it1.next(), it1) // We can redefine this LazyList such that all we have is the Iterator left // and allow the LazyList to be garbage collected as required. Using a def // to provide the LazyList ensures that no val is holding onto the head as // is the case with lazylist1 def lazylist2: LazyList[Int] = { def loop(v: Int): LazyList[Int] = v #:: loop(v + 1) loop(0) } val it2 = stream2.iterator() loop("Iterator2: ", it2.next(), it2) // And, of course, we don't actually need a LazyList at all for such a simple // problem. There's no reason to use a LazyList if you don't actually need // one. val it3 = new Iterator[Int] { var i = -1 def hasNext = true def next(): Int = { i += 1; i } } loop("Iterator3: ", it3.next(), it3)
- The fact that
tail
works at all is of interest. In the definition offibs
we have an initial(0, 1, LazyList(...))
sotail
is deterministic. If we definedfibs
such that only0
were concretely known then the act of determiningtail
would require the evaluation oftail
which would cause an infinite recursion and stack overflow. If we define a definition where the tail is not initially computable then we're going to have an infinite recursion:
// The first time we try to access the tail we're going to need more // information which will require us to recurse, which will require us to // recurse, which... lazy val sov: LazyList[Vector[Int]] = Vector(0) #:: sov.zip(sov.tail).map { n => n._1 ++ n._2 }
The definition of
fibs
above creates a larger number of objects than necessary depending on how you might want to implement it. The following implementation provides a more "cost effective" implementation due to the fact that it has a more direct route to the numbers themselves:lazy val fib: LazyList[Int] = { def loop(h: Int, n: Int): LazyList[Int] = h #:: loop(n, h + n) loop(1, 1) }
- A
the type of the elements contained in this stream.
- Version
1.1 08/08/03
- Since
2.8
- See also
"Scala's Collection Library overview" section on
Streams
for more information.
- The definition of
-
trait
LinearSeq[+A] extends Seq[A] with collection.LinearSeq[A] with LinearSeqOps[A, LinearSeq, LinearSeq[A]]
Base trait for immutable linear sequences that have efficient
head
andtail
- trait LinearSeqOps[+A, +CC[X] <: LinearSeq[X], +C <: LinearSeq[A]] extends SeqOps[A, CC, C] with collection.LinearSeqOps[A, CC, C]
-
sealed abstract
class
List[+A] extends LinearSeq[A] with LinearSeqOps[A, List, List[A]] with StrictOptimizedSeqOps[A, List, List[A]] with Serializable
A class for immutable linked lists representing ordered collections of elements of type
A
.A class for immutable linked lists representing ordered collections of elements of type
A
.This class comes with two implementing case classes
scala.Nil
andscala.::
that implement the abstract membersisEmpty
,head
andtail
.This class is optimal for last-in-first-out (LIFO), stack-like access patterns. If you need another access pattern, for example, random access or FIFO, consider using a collection more suited to this than
List
.Note: Despite being an immutable collection, the implementation uses mutable state internally during construction. These state changes are invisible in single-threaded code but can lead to race conditions in some multi-threaded scenarios. The state of a new collection instance may not have been "published" (in the sense of the Java Memory Model specification), so that an unsynchronized non-volatile read from another thread may observe the object in an invalid state (see scala/bug#7838 for details). Note that such a read is not guaranteed to ever see the written object at all, and should therefore not be used, regardless of this issue. The easiest workaround is to exchange values between threads through a volatile var.
Performance
Time:
List
hasO(1)
prepend and head/tail access. Most other operations areO(n)
on the number of elements in the list. This includes the index-based lookup of elements,length
,append
andreverse
.Space:
List
implements structural sharing of the tail list. This means that many operations are either zero- or constant-memory cost.val mainList = List(3, 2, 1) val with4 = 4 :: mainList // re-uses mainList, costs one :: instance val with42 = 42 :: mainList // also re-uses mainList, cost one :: instance val shorter = mainList.tail // costs nothing as it uses the same 2::1::Nil instances as mainList
// Make a list via the companion object factory val days = List("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") // Make a list element-by-element val when = "AM" :: "PM" :: Nil // Pattern match days match { case firstDay :: otherDays => println("The first day of the week is: " + firstDay) case Nil => println("There don't seem to be any week days.") }
- Version
2.8
- Since
1.0
- Note
The functional list is characterized by persistence and structural sharing, thus offering considerable performance and space consumption benefits in some scenarios if used correctly. However, note that objects having multiple references into the same functional list (that is, objects that rely on structural sharing), will be serialized and deserialized with multiple lists, one for each reference to it. I.e. structural sharing is lost after serialization/deserialization.
- See also
"Scala's Collection Library overview" section on
Lists
for more information.
Example: -
sealed
class
ListMap[K, +V] extends Map[K, V] with MapOps[K, V, ListMap, ListMap[K, V]] with StrictOptimizedIterableOps[(K, V), Iterable, ListMap[K, V]] with Serializable
This class implements immutable maps using a list-based data structure.
This class implements immutable maps using a list-based data structure. List map iterators and traversal methods visit key-value pairs in the order whey were first inserted.
Entries are stored internally in reversed insertion order, which means the newest key is at the head of the list. As such, methods such as
head
andtail
are O(n), whilelast
andinit
are O(1). Other operations, such as inserting or removing entries, are also O(n), which makes this collection suitable only for a small number of elements.Instances of
ListMap
represent empty maps; they can be either created by calling the constructor directly, or by applying the functionListMap.empty
.- K
the type of the keys contained in this list map
- V
the type of the values associated with the keys
- Annotations
- @SerialVersionUID()
- Version
2.0, 01/01/2007
- Since
1
-
sealed
class
ListSet[A] extends Set[A] with SetOps[A, ListSet, ListSet[A]] with StrictOptimizedIterableOps[A, ListSet, ListSet[A]] with Serializable
This class implements immutable sets using a list-based data structure.
This class implements immutable sets using a list-based data structure. List set iterators and traversal methods visit elements in the order whey were first inserted.
Elements are stored internally in reversed insertion order, which means the newest element is at the head of the list. As such, methods such as
head
andtail
are O(n), whilelast
andinit
are O(1). Other operations, such as inserting or removing entries, are also O(n), which makes this collection suitable only for a small number of elements.Instances of
ListSet
represent empty sets; they can be either created by calling the constructor directly, or by applying the functionListSet.empty
.- A
the type of the elements contained in this list set
- Annotations
- @SerialVersionUID()
- Version
1.0, 09/07/2003
- Since
1
-
sealed abstract
class
LongMap[+T] extends Map[Long, T] with MapOps[Long, T, Map, LongMap[T]] with StrictOptimizedIterableOps[(Long, T), Iterable, LongMap[T]]
Specialised immutable map structure for long keys, based on Fast Mergeable Long Maps by Okasaki and Gill.
Specialised immutable map structure for long keys, based on Fast Mergeable Long Maps by Okasaki and Gill. Essentially a trie based on binary digits of the integers.
Note: This class is as of 2.8 largely superseded by HashMap.
- T
type of the values associated with the long keys.
- Since
2.7
-
trait
Map[K, +V] extends Iterable[(K, V)] with collection.Map[K, V] with MapOps[K, V, Map, Map[K, V]]
Base type of immutable Maps
-
trait
MapOps[K, +V, +CC[X, +Y] <: MapOps[X, Y, CC, _], +C <: MapOps[K, V, CC, C]] extends IterableOps[(K, V), Iterable, C] with collection.MapOps[K, V, CC, C]
Base trait of immutable Maps implementations
-
sealed
class
NumericRange[T] extends IndexedSeq[T] with IndexedSeqOps[T, IndexedSeq, IndexedSeq[T]] with StrictOptimizedSeqOps[T, IndexedSeq, IndexedSeq[T]] with Serializable
NumericRange
is a more generic version of theRange
class which works with arbitrary types.NumericRange
is a more generic version of theRange
class which works with arbitrary types. It must be supplied with anIntegral
implementation of the range type.Factories for likely types include
Range.BigInt
,Range.Long
, andRange.BigDecimal
.Range.Int
exists for completeness, but theInt
-basedscala.Range
should be more performant.val r1 = new Range(0, 100, 1) val veryBig = Int.MaxValue.toLong + 1 val r2 = Range.Long(veryBig, veryBig + 100, 1) assert(r1 sameElements r2.map(_ - veryBig))
-
sealed abstract
class
Range extends IndexedSeq[Int] with IndexedSeqOps[Int, IndexedSeq, IndexedSeq[Int]] with StrictOptimizedSeqOps[Int, IndexedSeq, IndexedSeq[Int]] with Serializable
The
Range
class represents integer values in range [start;end) with non-zero step valuestep
.The
Range
class represents integer values in range [start;end) with non-zero step valuestep
. It's a special case of an indexed sequence. For example:val r1 = 0 until 10 val r2 = r1.start until r1.end by r1.step + 1 println(r2.length) // = 5
Ranges that contain more than
Int.MaxValue
elements can be created, but these overfull ranges have only limited capabilities. Any method that could require a collection of overInt.MaxValue
length to be created, or could be asked to index beyondInt.MaxValue
elements will throw an exception. Overfull ranges can safely be reduced in size by changing the step size (e.g.by 3
) or taking/dropping elements.contains
,equals
, and access to the ends of the range (head
,last
,tail
,init
) are also permitted on overfull ranges.- Annotations
- @SerialVersionUID()
- trait Seq[+A] extends Iterable[A] with collection.Seq[A] with SeqOps[A, Seq, Seq[A]]
- trait SeqOps[+A, +CC[_], +C] extends collection.SeqOps[A, CC, C]
-
trait
Set[A] extends Iterable[A] with collection.Set[A] with SetOps[A, Set, Set[A]]
Base trait for immutable set collections
-
trait
SetOps[A, +CC[X], +C <: SetOps[A, CC, C]] extends collection.SetOps[A, CC, C]
Base trait for immutable set operations
- trait SortedMap[K, +V] extends Map[K, V] with collection.SortedMap[K, V] with SortedMapOps[K, V, SortedMap, SortedMap[K, V]]
- trait SortedMapOps[K, +V, +CC[X, +Y] <: Map[X, Y] with SortedMapOps[X, Y, CC, _], +C <: SortedMapOps[K, V, CC, C]] extends MapOps[K, V, Map, C] with collection.SortedMapOps[K, V, CC, C]
-
trait
SortedSet[A] extends Set[A] with collection.SortedSet[A] with SortedSetOps[A, SortedSet, SortedSet[A]]
Base trait for sorted sets
- trait SortedSetOps[A, +CC[X] <: SortedSet[X], +C <: SortedSetOps[A, CC, C]] extends SetOps[A, Set, C] with collection.SortedSetOps[A, CC, C]
-
trait
StrictOptimizedSeqOps[+A, +CC[_], +C] extends SeqOps[A, CC, C] with collection.StrictOptimizedSeqOps[A, CC, C]
Trait that overrides operations to take advantage of strict builders.
- type StringOps = collection.StringOps
- type StringView = collection.StringView
-
final
class
TreeMap[K, +V] extends SortedMap[K, V] with SortedMapOps[K, V, TreeMap, TreeMap[K, V]] with StrictOptimizedIterableOps[(K, V), Iterable, TreeMap[K, V]] with Serializable
This class implements immutable maps using a tree.
This class implements immutable maps using a tree.
- K
the type of the keys contained in this tree map.
- V
the type of the values associated with the keys.
- Annotations
- @SerialVersionUID()
- Version
1.1, 03/05/2004
- Since
1
- See also
"Scala's Collection Library overview" section on
Red-Black Trees
for more information.
-
final
class
TreeSet[A] extends SortedSet[A] with SortedSetOps[A, TreeSet, TreeSet[A]] with StrictOptimizedIterableOps[A, Set, TreeSet[A]] with StrictOptimizedSortedSetOps[A, TreeSet, TreeSet[A]]
This class implements immutable sorted sets using a tree.
This class implements immutable sorted sets using a tree.
- A
the type of the elements contained in this tree set
- Version
2.0, 02/01/2007
- Since
1
- See also
"Scala's Collection Library overview" section on
Red-Black Trees
for more information.
-
final
class
Vector[+A] extends IndexedSeq[A] with IndexedSeqOps[A, Vector, Vector[A]] with StrictOptimizedSeqOps[A, Vector, Vector[A]] with VectorPointer[A] with Serializable
Vector is a general-purpose, immutable data structure.
Vector is a general-purpose, immutable data structure. It provides random access and updates in effectively constant time, as well as very fast append and prepend. Because vectors strike a good balance between fast random selections and fast random functional updates, they are currently the default implementation of immutable indexed sequences. It is backed by a little endian bit-mapped vector trie with a branching factor of 32. Locality is very good, but not contiguous, which is good for very large sequences.
Note: Despite being an immutable collection, the implementation uses mutable state internally during construction. These state changes are invisible in single-threaded code but can lead to race conditions in some multi-threaded scenarios. The state of a new collection instance may not have been "published" (in the sense of the Java Memory Model specification), so that an unsynchronized non-volatile read from another thread may observe the object in an invalid state (see scala/bug#7838 for details). Note that such a read is not guaranteed to ever see the written object at all, and should therefore not be used, regardless of this issue. The easiest workaround is to exchange values between threads through a volatile var.
- A
the element type
- Annotations
- @SerialVersionUID()
- See also
"Scala's Collection Library overview" section on
Vectors
for more information.
-
final
class
VectorBuilder[A] extends ReusableBuilder[A, Vector[A]] with VectorPointer[A]
A class to build instances of
Vector
.A class to build instances of
Vector
. This builder is reusable. - class VectorIterator[+A] extends AbstractIterator[A] with VectorPointer[A]
-
final
class
WrappedString extends AbstractSeq[Char] with IndexedSeq[Char]
This class serves as a wrapper augmenting
String
s with all the operations found in indexed sequences.This class serves as a wrapper augmenting
String
s with all the operations found in indexed sequences.The difference between this class and
StringOps
is that calling transformer methods such asfilter
andmap
will yield an object of typeWrappedString
rather than aString
.- Since
2.8
-
type
Traversable[+X] = Iterable[X]
- Annotations
- @deprecated
- Deprecated
(Since version 2.13.0) Use Iterable instead of Traversable
Value Members
- val StringOps: collection.StringOps.type
- val StringView: collection.StringView.type
-
object
BitSet extends SpecificIterableFactory[Int, BitSet] with Serializable
This object provides a set of operations to create
immutable.BitSet
values. -
object
HashMap extends MapFactory[HashMap] with Serializable
This object provides a set of operations to create
immutable.HashMap
values. -
object
HashSet extends IterableFactory[HashSet] with Serializable
This object provides a set of operations to create
immutable.HashSet
values. -
object
ImmutableArray extends StrictOptimizedSeqFactory[ImmutableArray]
This object provides a set of operations to create
ImmutableArray
values. - object IndexedSeq extends Delegate[IndexedSeq]
-
object
IntMap
A companion object for integer maps.
- object Iterable extends Delegate[Iterable]
-
object
LazyList extends SeqFactory[LazyList]
This object provides a set of operations to create
LazyList
values. -
object
List extends StrictOptimizedSeqFactory[List] with Serializable
This object provides a set of operations to create
List
values. -
object
ListMap extends MapFactory[ListMap] with Serializable
This object provides a set of operations to create ListMap values.
This object provides a set of operations to create ListMap values.
Note that each element insertion takes O(n) time, which means that creating a list map with n elements will take O(n2) time. This makes the builder suitable only for a small number of elements.
- Since
1
- See also
"Scala's Collection Library overview" section on
List Maps
for more information.
-
object
ListSet extends IterableFactory[ListSet] with Serializable
This object provides a set of operations to create ListSet values.
This object provides a set of operations to create ListSet values.
Note that each element insertion takes O(n) time, which means that creating a list set with n elements will take O(n2) time. This makes the builder suitable only for a small number of elements.
- Since
1
-
object
LongMap
A companion object for long maps.
-
object
Map extends MapFactory[Map]
This object provides a set of operations to create
immutable.Map
values. -
object
Nil extends List[Nothing] with Product with Serializable
- Annotations
- @SerialVersionUID()
-
object
NumericRange extends Serializable
A companion object for numeric ranges.
-
object
Range extends Serializable
Companion object for ranges.
-
object
Seq extends Delegate[Seq]
This object provides a set of operations to create
immutable.Seq
values. -
object
Set extends IterableFactory[Set]
This object provides a set of operations to create
immutable.Set
values. - object SortedMap extends Delegate[SortedMap]
-
object
SortedSet extends Delegate[SortedSet]
This object provides a set of operations to create
immutable.SortedSet
values. -
object
TreeMap extends SortedMapFactory[TreeMap] with Serializable
This object provides a set of operations to create immutable.TreeMap values.
-
object
TreeSet extends SortedIterableFactory[TreeSet]
This object provides a set of operations to create
immutable.TreeSet
values. -
object
Vector extends StrictOptimizedSeqFactory[Vector] with Serializable
This object provides a set of operations to create
Vector
values. -
object
WrappedString extends SpecificIterableFactory[Char, WrappedString]
A companion object for wrapped strings.
A companion object for wrapped strings.
- Since
2.8