Traversable是所有collections的父類
Traversable是所有集合層級的最頂端的,他只有一個抽象方法, foreach:
def foreach[U](f: Elem => U)
實現Traversable只需要定義這個方法,其他方法可以直接繼承得到。
foreach
方法以爲遍歷集合中的所有元素,並且應用傳入的方法,f , 對每一個元素。 函數(或者方法,操作)的類型是Elem => U
, Elem
是集合元素的類型 而 U是一個任意的結果類型. 函數的f的調用是他的一個副作用,事實上函數的結果都會被 foreach方法丟棄。
函數分類:Traversable
的方法可以分類如下:
Addition:
方法名: ++
說明: 同時追加兩個traversable,或者追加一個迭代的所有元素到traversable
Map :
方法名: map, flatMap, collect
說明: 通過應用函數到集合的元素中,來產生一個新的集合
Conversions :
方法名:toArray
, toList
, toIterable
, toSeq
, toIndexedSeq
, toStream
, toSet
,toMap
說明: 使得 Traversable
變的更加具體。所有這些轉換返回的接收器的參數不會改變,如果集合的運行時類型已經匹配了要求的集合類型。 如: 在一個列表中應用 toList,將會調用該列表的 toList方法。
Copying operations :
方法名 : copyToBuffer
,
copyToArray
說明:作用如他們的名字
Size info:
方法名:isEmpty
, nonEmpty
, size
, and hasDefiniteSize
:
說明:作用如其名,唯一需要說明的是hasDefiniteSize
,判斷一個集合是否有窮還是無窮
Element retrieval:
方法名:head
, last
, headOption
, lastOption
, and find
.
說明: 頭尾方法的定義. 但是有些集合不能很好的定義頭尾方法。 如,hash存儲是key,value, 又如某些運行時會不斷改變元素順序的。
Sub-collection retrieval operations :
方法名: tail
, init
, slice
, take
, drop
, takeWhile
,dropWhile
, filter
, filterNot
, withFilter
.
說明:返回子集合
Subdivision operations :
方法名: plitAt
, span
, partition
, groupBy
說明:
分割集合成幾個子集合
Element tests:
方法名:
exists
, forall
, count
說明:測試斷言(書生:即方法做的事情)是否成立
Folds :
方法名:
foldLeft
, foldRight
, /:
, :\
, reduceLeft
, reduceRight
說明:用二進制的方式操作連續的元素
Specific folds :
方法名:
sum
, product
, min
, max
說明:用於特定類型,如數字
String :
方法名:
mkString
, addString
, stringPrefix
說明:轉換一個集合到字符串的方式
View:
方法名: view
說明:這個方法將單獨介紹
Class Traversable中的方法
WHAT IT IS | WHAT IT DOES |
---|---|
Abstract Method: | |
xs foreach f
|
Executes function f for every element of xs .
|
Addition: | |
xs ++ ys
|
A collection consisting of the elements of both xs and ys . ys is a TraversableOnce collection, i.e., either a Traversable or an Iterator.
|
Maps: | |
xs map f
|
The collection obtained from applying the function f to every element in xs .
|
xs flatMap f
|
The collection obtained from applying the collection-valued function f to every element in xs and concatenating the results.
|
xs collect f
|
The collection obtained from applying the partial function f to every element in xs for which it is defined and collecting the results.
|
Conversions: | |
xs.toArray
|
Converts the collection to an array. |
xs.toList
|
Converts the collection to a list. |
xs.toIterable
|
Converts the collection to an iterable. |
xs.toSeq
|
Converts the collection to a sequence. |
xs.toIndexedSeq
|
Converts the collection to an indexed sequence. |
xs.toStream
|
Converts the collection to a lazily computed stream. |
xs.toSet
|
Converts the collection to a set. |
xs.toMap
|
Converts the collection of key/value pairs to a map. If the collection does not have pairs as elements, calling this operation results in a static type error. |
Copying: | |
xs copyToBuffer buf
|
Copies all elements of the collection to buffer buf .
|
xs copyToArray(arr, s, n)
|
Copies at most n elements of the collection to array arr starting at index s . The last two arguments are optional.
|
Size info: | |
xs.isEmpty
|
Tests whether the collection is empty. |
xs.nonEmpty
|
Tests whether the collection contains elements. |
xs.size
|
The number of elements in the collection. |
xs.hasDefiniteSize
|
True if xs is known to have finite size.
|
Element Retrieval: | |
xs.head
|
The first element of the collection (or, some element, if no order is defined). |
xs.headOption
|
The first element of xs in an option value, or None if xs is empty.
|
xs.last
|
The last element of the collection (or, some element, if no order is defined). |
xs.lastOption
|
The last element of xs in an option value, or None if xs is empty.
|
xs find p
|
An option containing the first element in xs that satisfies p , or None if no element qualifies.
|
Subcollections: | |
xs.tail
|
The rest of the collection except xs.head .
|
xs.init
|
The rest of the collection except xs.last .
|
xs slice (from, to)
|
A collection consisting of elements in some index range of xs (from from up to, and excluding to ).
|
xs take n
|
A collection consisting of the first n elements of xs (or, some arbitrary n elements, if no order is defined).
|
xs drop n
|
The rest of the collection except xs take n .
|
xs takeWhile p
|
The longest prefix of elements in the collection that all satisfy p .
|
xs dropWhile p
|
The collection without the longest prefix of elements that all satisfy p .
|
xs filter p
|
The collection consisting of those elements of xs that satisfy the predicate p .
|
xs withFilter p
|
A non-strict filter of this collection. Subsequent calls to map , flatMap , foreach , and withFilter will only apply to those elements of xs for which the condition p is true.
|
xs filterNot p
|
The collection consisting of those elements of xs that do not satisfy the predicate p .
|
Subdivisions: | |
xs splitAt n
|
Split xs at a position, giving the pair of collections (xs take n, xs drop n) .
|
xs span p
|
Split xs according to a predicate, giving the pair of collections (xs takeWhile p, xs.dropWhile p) .
|
xs partition p
|
Split xs into a pair of two collections; one with elements that satisfy the predicate p , the other with elements that do not, giving the pair of collections (xs filter p, xs.filterNot p)
|
xs groupBy f
|
Partition xs into a map of collections according to a discriminator function f .
|
Element Conditions: | |
xs forall p
|
A boolean indicating whether the predicate p holds for all elements of xs .
|
xs exists p
|
A boolean indicating whether the predicate p holds for some element in xs .
|
xs count p
|
The number of elements in xs that satisfy the predicate p .
|
Folds: | |
(z /: xs)(op)
|
Apply binary operation op between successive elements of xs , going left to right and starting with z .
|
(xs :\ z)(op)
|
Apply binary operation op between successive elements of xs , going right to left and starting with z .
|
xs.foldLeft(z)(op)
|
Same as (z /: xs)(op) .
|
xs.foldRight(z)(op)
|
Same as (xs :\ z)(op) .
|
xs reduceLeft op
|
Apply binary operation op between successive elements of non-empty collection xs , going left to right.
|
xs reduceRight op
|
Apply binary operation op between successive elements of non-empty collection xs , going right to left.
|
Specific Folds: | |
xs.sum
|
The sum of the numeric element values of collection xs .
|
xs.product
|
The product of the numeric element values of collection xs .
|
xs.min
|
The minimum of the ordered element values of collection xs .
|
xs.max
|
The maximum of the ordered element values of collection xs .
|
Strings: | |
xs addString (b, start, sep, end)
|
Adds a string to StringBuilder b that shows all elements of xs between separators sep enclosed in strings start and end . start , sep , end are all optional.
|
xs mkString (start, sep, end)
|
Converts the collection to a string that shows all elements of xs between separators sep enclosed in strings start and end . start , sep , end are all optional.
|
xs.stringPrefix
|
The collection name at the beginning of the string returned from xs.toString .
|
Views: | |
xs.view
|
Produces a view over xs .
|
xs view (from, to)
|
Produces a view that represents the elements in some index range of xs .
|