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

Seq

Seq is probably the most ubiquitous collection in the library. Like Map, it has a notion of succession of elements and the elements have indices. It is defined as trait Seq[+A] extends Iterable[A] with PartialFunction[Int, A] with SeqOps[A, Seq, Seq[A]] with Equals. Also similar to map, Seq specifies support for the equality relation and also extends PartialFunction, which accepts an index of the element as a parameter. As there are a lots of classes implementing Seq, we will take a gradual approach and look at them level by level. The direct children of Seq are shown in the following diagram:

scala.Seq, known from previous Scala versions, is now replaced by scala.collection.immutable.Seq

As with other collections, SeqOps extend IterableOps by adding quite a few methods:

  • Element retrieval: apply retrieves an element with a given index.
  • Indexing and search: segmentLength, isDefinedAt, indexWhere, indexOf, lastIndexOf, lastIndexWhere, indexOfSlice, lastIndexOfSlice, containsSlice, containsstartsWith, endsWithindices, and search. These methods allow us to retrieve information about the presence or indexes of elements or subsequences given some predicate or element value.
  • Size: length and lengthCompare provide efficient operations to retrieve the length of the collection.
  • Addition: prepend, +, appended, :+, prependAll, ++appendedAll, :++, concat, and union. Can be used to append or prepend one or multiple elements to the collection.
  • Filtering: distinct and distinctBy remove duplicates, possibly given some predicate.
  • Reversal: reverse and reverseIterator return a new collection with elements in the reversed order.
  • Sorting: sortedsortWith, and sortBy sort this collection by some implicit Ordering, or by a given function, or both.
  • Equality: sameElements and corresponds check whether this collection contains the same elements in the same order as given, using equality-checking or the provided comparison function.
  • Subcollection retrieval: permutations and combinationsThese methods allow us to retrieve a subcollection(s) that satisfies given conditions.
  • Updates: diff, intersect, patch, updated, and update (mutable). Modify elements of this collection using another collection or element and returning another collection (except the last method defined on mutable.Seq, which update elements in place).

Each of the Seq direct descendants has its own specific properties and a subtree of implementations. We'll breeze through them now. 

主站蜘蛛池模板: 庆城县| 观塘区| 东山县| 昭苏县| 长兴县| 金坛市| 安达市| 景德镇市| 耒阳市| 永康市| 新竹县| 秦安县| 武穴市| 马边| 南乐县| 庐江县| 武安市| 滨州市| 新晃| 怀仁县| 五河县| 宜都市| 清涧县| 隆德县| 慈溪市| 桂阳县| 黎川县| 江津市| 攀枝花市| 神池县| 香格里拉县| 鹿泉市| 平顺县| 东乌珠穆沁旗| 高要市| 渝中区| 平安县| 高清| 易门县| 泰宁县| 伽师县|