The sequence traits Seq, IndexedSeq, and LinearSeqBuffersContents

Buffers

An important sub-category of mutable sequences is Buffers. They allow not only updates of existing elements but also element insertions, element removals, and efficient additions of new elements at the end of the buffer. The principal new methods supported by a buffer are += and ++= for element addition at the end, +=: and ++=: for addition at the front, insert and insertAll for element insertions, as well as remove and -= for element removal. These operations are summarized in the following table.

Two often used implementations of buffers are ListBuffer and ArrayBuffer. As the name implies, a ListBuffer is backed by a List, and supports efficient conversion of its elements to a List, whereas an ArrayBuffer is backed by an array, and can be quickly converted into one.

Operations in class Buffer  
 

What it is

What it does

Additions:

buf += x Appends element x to buffer, and returns buf itself as result.
buf += (x, y, z) Appends given elements to buffer.
buf ++= xs Appends all elements in xs to buffer.
x +=: buf Prepends element x to buffer.
xs ++=: buf Prepends all elements in xs to buffer.
buf insert (i, x) Inserts element x at index i in buffer.
buf insertAll (i, xs) Inserts all elements in xs at index i in buffer.

Removals:

buf -= x Removes element x from buffer.
buf remove i Removes element at index i from buffer.
buf remove (i, n) Removes n elements starting at index i from buffer.
buf trimStart n Removes first n elements from buffer.
buf trimEnd n Removes last n elements from buffer.
buf.clear() Removes all elements from buffer.

Cloning:

buf.clone A new buffer with the same elements as buf.

Next: Sets


The sequence traits Seq, IndexedSeq, and LinearSeqBuffersContents