官术网_书友最值得收藏!

  • Learn Scala Programming
  • Slava Schmidt
  • 374字
  • 2021-06-10 19:35:42

Buffers

Buffers conclude our rush through the collection library. In essence, Buffer is just a Seq that can grow and shrink. This sub-hierarchy exists only in immutable form, though IndexedBuffer inherits from both Buffer and IndexedSeq, as shown by the next diagram:


Let's take a look at the methods that these collections define, in addition to the definitions inherited from SeqOps:

  • Buffer defines methods to add or remove one or more elements in place or returning new buffer: prepend, append, appendAll+=:, prependAll++=:, insertinsertAll, remove, subtractOne, trimStart, trimEnd, patchInPlace, dropInPlace, dropRightInPlace, takeRightInPlace, sliceInPlace, dropWhileInPlace, takeWhileInPlace, and padToInPlace.
  • ListBuffer is a concrete buffer implementation baked by a List. In addition to other discussed methods, it provides prependToList, which allows us to prepend this collection to another list and a triple of mapInPlace, flatMapInPlace, and filterInPlace, giving us a possibility to modify elements in place.
  • Take a Buffer, add an IndexedSeq, and you'll get an IndexedBuffer. Similar to ListBuffer, it provides the flatMapInPlace and filterInPlace methods.
  • ArrayBuffer is a concrete implementation of IndexedBuffer that uses an array to store its elements and has a constant time for append, update, and random access. It has a sizeHint method, which can be used to enlarge the underlying array. It is a default implementation instantiated if mutable.Seq is created.
  • ArrayDeque is another efficient collection that emerged in the 2.13 version. It implements a double-ended queue that internally uses a resizable circular buffer. This allows to have a constant time for append, prepend, remove first, remove last, and random-access operations. There are lots of additional methods available on this collection, mostly because of the notion of the second-end: removeHeadOption, removeHead, removeLastOption, removeLast, removeAll, removeAllReverse, removeHeadWhile, removeLastWhile, removeFirst, removeAll, clearAndShrink, copySliceToArray, and trimToSize.
  • The Queue in this hierarchy is mutable. It is based on ArrayDeque and allows us to insert and retrieve elements in the FIFO manner. The following methods are available for that: enqueue, enqueueAll, dequeue, dequeueFirst, dequeueAll, and dequeueWhile.
  • The Stack is similar to Queue, but it implements in last-in-first-out (LIFO) order instead of FIFO. The methods it defines are formulated in the corresponding terms: push, pushAll, pop, popAll, popWhile, and top.
主站蜘蛛池模板: 包头市| 武威市| 综艺| 永州市| 四平市| 开原市| 怀远县| 都安| 全南县| 综艺| 上栗县| 云梦县| 阳泉市| 临武县| 富宁县| 太白县| 西城区| 张家川| 黄平县| 佳木斯市| 雅安市| 西峡县| 宁河县| 繁昌县| 宣化县| 宝丰县| 嘉义市| 新兴县| 宁蒗| 长阳| 天津市| 旌德县| 香河县| 隆尧县| 宣化县| 新野县| 富顺县| 留坝县| 洪雅县| 丰顺县| 卓资县|