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. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.44.182