Scala 2.8 集合设计教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1722137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala 2.8 collections design tutorial
提问by oxbow_lakes
Following on from my breathless confusion, what are some good resources which explain how the new Scala2.8collections library has been structured. I'm interested to find some information on how the following fit together:
继我令人窒息的困惑之后,有哪些很好的资源可以解释新的Scala 2.8集合库是如何构建的。我有兴趣找到一些有关以下内容如何组合的信息:
- The collection classes/traits themselves (e.g.
List,Iterable) - Why the Likeclasses exist (e.g.
TraversableLike) - What the companion methods are for (e.g.
List.companion) - How I know what
implicitobjects are in scope at a given point
- 集合类/特征本身(例如
List,Iterable) - 为什么Like类存在(例如
TraversableLike) - 配套方法的用途(例如
List.companion) - 我如何知道
implicit在给定点有哪些对象在范围内
回答by Daniel C. Sobral
Foreword
前言
There's a 2.8 collection walk-throughby Martin Odersky which should probably be your first reference. It has been supplemented as well with architectural notes, which will be of particular interest to those who want to design their own collections.
Martin Odersky有一个2.8 集合演练,这可能是您的第一个参考。它还补充了建筑注释,对于那些想要设计自己的系列的人来说,这将是特别感兴趣的。
The rest of this answer was written way before any such thing existed (in fact, before 2.8.0 itself was released).
这个答案的其余部分是在任何此类事情存在之前编写的(实际上,在 2.8.0 本身发布之前)。
You can find a paper about it as Scala SID #3. Other papers in that area should be interesting as well to people interested in the differences between Scala 2.7 and 2.8.
您可以在Scala SID #3 中找到有关它的论文。对于那些对 Scala 2.7 和 2.8 之间的差异感兴趣的人来说,该领域的其他论文也应该很有趣。
I'll quote from the paper, selectively, and complement with some thoughts of mine. There are also some images, generated by Matthias at decodified.com, and the original SVG files can be found here.
我将有选择地引用论文,并补充我的一些想法。还有一些图像,由 Matthias 在 decodeified.com 上生成,原始 SVG 文件可以在这里找到。
The collection classes/traits themselves
集合类/特征本身
There are actually three hierarchies of traits for the collections: one for mutable collections, one for immutable collections, and one which doesn't make any assumptions about the collections.
实际上,集合的特征分为三种层次结构:一种用于可变集合,一种用于不可变集合,另一种对集合不做任何假设。
There's also a distinction between parallel, serial and maybe-parallel collections, which was introduced with Scala 2.9. I'll talk about them in the next section. The hierarchy described in this section refers exclusively to non-parallel collections.
Scala 2.9 引入了并行、串行和可能并行集合之间的区别。我将在下一节中讨论它们。本节中描述的层次结构专指非并行集合。
The following image shows the non-specific hierarchy introduced with Scala 2.8:

下图显示了 Scala 2.8 引入的非特定层次结构:

All elements shown are traits. In the other two hierarchies there are also classes directly inheriting the traits as well as classes which can be viewed asbelonging in that hierarchy through implicit conversion to wrapper classes. The legend for these graphs can be found after them.
显示的所有元素都是特征。在其他两个层次结构中,也有直接继承特征的类以及通过隐式转换为包装类可以被视为属于该层次结构的类。这些图表的图例可以在它们之后找到。
Graph for immutable hierarchy:

不可变层次结构图:

Graph for mutable hierarchy:

可变层次结构图:

Legend:
传奇:


Here's an abbreviated ASCII depiction of the collection hierarchy, for those who can't see the images.
这是集合层次结构的缩写 ASCII 描述,适用于那些看不到图像的人。
Traversable
|
|
Iterable
|
+------------------+--------------------+
Map Set Seq
| | |
| +----+----+ +-----+------+
Sorted Map SortedSet BitSet Buffer Vector LinearSeq
Parallel Collections
并行集合
When Scala 2.9 introduced parallel collections, one of the design goals was to make their use as seamless as possible. In the simplest terms, one can replace a non-parallel (serial) collection with a parallel one, and instantly reap the benefits.
当 Scala 2.9 引入并行集合时,设计目标之一是尽可能无缝地使用它们。用最简单的术语来说,可以用并行集合替换非并行(串行)集合,并立即获得好处。
However, since all collections until then were serial, many algorithms using them assumed and depended on the fact that they wereserial. Parallel collections fed to the methods with such assumptions would fail. For this reason, all the hierarchy described in the previous section mandates serial processing.
但是,由于在此之前的所有集合都是串行的,因此许多使用它们的算法假定并依赖于它们是串行的这一事实。提供给具有此类假设的方法的并行集合将失败。出于这个原因,上一节中描述的所有层次结构都要求串行处理。
Two new hierarchies were created to support the parallel collections.
创建了两个新的层次结构来支持并行集合。
The parallel collections hierarchy has the same names for traits, but preceded with Par: ParIterable, ParSeq, ParMapand ParSet. Note that there is no ParTraversable, since any collection supporting parallel access is capable of supporting the stronger ParIterabletrait. It doesn't have some of the more specialized traits present in the serial hierarchy either. This whole hierarchy is found under the directory scala.collection.parallel.
并行集合层次具有的性状相同的名称,但前面有Par:ParIterable,ParSeq,ParMap和ParSet。请注意,没有ParTraversable,因为任何支持并行访问的集合都能够支持更强的ParIterable特征。它也没有序列层次结构中存在的一些更专业的特征。整个层次结构都在目录下scala.collection.parallel。
The classes implementing parallel collections also differ, with ParHashMapand ParHashSetfor both mutable and immutable parallel collections, plus ParRangeand ParVectorimplementing immutable.ParSeqand ParArrayimplementing mutable.ParSeq.
实现并行的集合类也各不相同,有ParHashMap和ParHashSet两个可变和不可变的并行收集,加上ParRange与ParVector实施immutable.ParSeq和ParArray执行mutable.ParSeq。
Another hierarchy also exists that mirrors the traits of serial and parallel collections, but with a prefix Gen: GenTraversable, GenIterable, GenSeq, GenMapand GenSet. These traits are parentsto both parallel and serial collections. This means that a method taking a Seqcannot receive a parallel collection, but a method taking a GenSeqis expected to work with both serial and parallel collections.
另一个层次还存在镜子串行和并行集合的特点,但有一个前缀Gen:GenTraversable,GenIterable,GenSeq,GenMap和GenSet。这些特征是并行和串行集合的父级。这意味着采用 a 的方法Seq不能接收并行集合,但采用 a 的方法GenSeq预计可用于串行和并行集合。
Given the way these hierarchies were structured, code written for Scala 2.8 was fully compatible with Scala 2.9, and demanded serial behavior. Without being rewritten, it cannot take advantage of parallel collections, but the changes required are very small.
鉴于这些层次结构的结构方式,为 Scala 2.8 编写的代码与 Scala 2.9 完全兼容,并且需要串行行为。没有被重写,它无法利用并行集合,但所需的更改非常小。
Using Parallel Collections
使用并行集合
Any collection can be converted into a parallel one by calling the method paron it. Likewise, any collection can be converted into a serial one by calling the method seqon it.
任何集合都可以通过调用其par上的方法转换为并行集合。同样,任何集合都可以通过调用其seq上的方法转换为串行集合。
If the collection was already of the type requested (parallel or serial), no conversion will take place. If one calls seqon a parallel collection or paron a serial collection, however, a new collection with the requested characteristic will be generated.
如果集合已经是请求的类型(并行或串行),则不会发生转换。但是,如果调用seq并行集合或par串行集合,则会生成具有所请求特征的新集合。
Do not confuse seq, which turns a collection into a non-parallel collection, with toSeq, which returns a Seqcreated from the elements of the collection. Calling toSeqon a parallel collection will return a ParSeq, not a serial collection.
不要混淆seq,它将一个集合变成一个非并行集合,而toSeq,它返回一个Seq从集合的元素中创建的。调用toSeq并行集合将返回一个ParSeq,而不是串行集合。
The Main Traits
主要特征
While there are many implementing classes and subtraits, there are some basic traits in the hierarchy, each of which providing more methods or more specific guarantees, but reducing the number of classes that could implement them.
虽然有许多实现类和子特性,但层次结构中有一些基本特性,每个特性都提供更多方法或更具体的保证,但减少了可以实现它们的类的数量。
In the following subsections, I'll give a brief description of the main traits and the idea behind them.
在下面的小节中,我将简要描述主要特征及其背后的想法。
Trait TraversableOnce
特性 TraversableOnce
This trait is pretty much like trait Traversabledescribed below, but with the limitation that you can only use it once. That is, any methods called on a TraversableOncemayrender it unusable.
这个 traitTraversable与下面描述的trait 非常相似,但有一个限制,你只能使用它一次。也就是说,在 a 上调用的任何方法都TraversableOnce可能使其无法使用。
This limitation makes it possible for the same methods to be shared between the collections and Iterator. This makes it possible for a method that works with an Iteratorbut not using Iterator-specific methods to actually be able to work with any collection at all, plus iterators, if rewritten to accept TraversableOnce.
此限制使得可以在集合和Iterator. 这使得Iterator使用Iterator特定方法但不使用特定方法的方法实际上能够使用任何集合,加上迭代器,如果重写为 accept TraversableOnce。
Because TraversableOnceunifies collections and iterators, it does not appear in the previous graphs, which concern themselves only with collections.
因为TraversableOnce统一了集合和迭代器,所以没有出现在前面只关心集合的图中。
Trait Traversable
可遍历性状
At the top of the collectionhierarchy is trait Traversable. Its only abstract operation is
集合层次结构的顶部是 trait Traversable。它唯一的抽象操作是
def foreach[U](f: Elem => U)
The operation is meant to traverse all elements of the collection, and apply the given operation f to each element. The application is done for its side effect only; in fact any function result of f is discarded by foreach.
该操作旨在遍历集合的所有元素,并将给定的操作 f 应用于每个元素。申请只是为了它的副作用;事实上,f 的任何函数结果都会被 foreach 丢弃。
Traversible objects can be finite or infinite. An example of an infinite traversable object is the stream
of natural numbers Stream.from(0). The method hasDefiniteSizeindicates whether a collection is possibly
infinite. If hasDefiniteSizereturns true, the collection is certainly finite. If it returns false, the
collection has not been not fully elaborated yet, so it might be infinite or finite.
可遍历的对象可以是有限的或无限的。一个无限可遍历对象的例子是自然数流Stream.from(0)。该方法hasDefiniteSize指示集合是否可能是无限的。如果hasDefiniteSize返回 true,则集合肯定是有限的。如果它返回 false,则集合还没有完全阐述,因此它可能是无限的或有限的。
This class defines methods which can be efficiently implemented in terms of foreach(over 40 of them).
这个类定义了可以有效实现的方法foreach(超过 40 个)。
Trait Iterable
可迭代性状
This trait declares an abstract method iteratorthat returns an iterator that yields all the collection's elements one by one. The foreachmethod in Iterableis implemented in terms of iterator. Subclasses of Iterableoften override foreach with a direct implementation for efficiency.
这个 trait 声明了一个抽象方法iterator,该方法返回一个迭代器,该迭代器一个一个地产生集合的所有元素。中的foreach方法Iterable是根据 实现的iterator。Iterable为了提高效率,子类通常会使用直接实现来覆盖 foreach。
Class Iterablealso adds some less-often used methods to Traversable, which can be implemented efficiently only if an iteratoris available. They are summarized below.
ClassIterable还向 中添加了一些不太常用的方法Traversable,只有当 aniterator可用时才能有效实现。它们总结如下。
xs.iterator An iterator that yields every element in xs, in the same order as foreach traverses elements.
xs takeRight n A collection consisting of the last n elements of xs (or, some arbitrary n elements, if no order is defined).
xs dropRight n The rest of the collection except xs takeRight n.
xs sameElements ys A test whether xs and ys contain the same elements in the same order
Other Traits
其他特性
After Iterablethere come three base traits which inherit from it: Seq, Set, and Map. All three have an applymethod and all three implement the PartialFunctiontrait, but the meaning of applyis different in each case.
后Iterable有来了三个基本的特征,其继承它:Seq,Set,和Map。这三个都有一个apply方法,三个都实现了PartialFunctiontrait,但是apply每种情况下的含义是不同的。
I trust the meaning of Seq, Setand Mapis intuitive. After them, the classes break up in specific implementations that offer particular guarantees with regards to performance, and the methods it makes available as a result of it. Also available are some traits with further refinements, such as LinearSeq, IndexedSeqand SortedSet.
我相信 , 的含义Seq,Set并且Map是直观的。在它们之后,这些类在特定的实现中分解,这些实现提供了关于性能的特定保证,以及它作为结果可用的方法。还提供了一些进一步改进的特征,例如LinearSeq,IndexedSeq和SortedSet。
The listing below may be improved. Leave a comment with suggestions and I'll fix it.
下面的列表可能会得到改进。留下评论和建议,我会修复它。
Base Classes and Traits
基类和特征
Traversable-- Basic collection class. Can be implemented just withforeach.TraversableProxy-- Proxy for aTraversable. Just pointselfto the real collection.TraversableView-- A Traversable with some non-strict methods.TraversableForwarder-- Forwards most methods tounderlying, excepttoString,hashCode,equals,stringPrefix,newBuilder,viewand all calls creating a new iterable object of the same kind.mutable.Traversableandimmutable.Traversable-- same thing asTraversable, but restricting the collection type.- Other special-cases
Iterableclasses, such asMetaData, exists. Iterable-- A collection for which anIteratorcan be created (throughiterator).IterableProxy,IterableView,mutableandimmutable.Iterable.
Iterator-- A trait which is not descendant ofTraversable. DefinenextandhasNext.CountedIterator-- AnIteratordefiningcount, which returns the elements seen so far.BufferedIterator-- Defineshead, which returns the next element without consuming it.- Other special-cases
Iteratorclasses, such asSource, exists.
Traversable-- 基础收藏类。可以只用foreach.TraversableProxy- 代理一个Traversable. 只需指向self真正的收藏。TraversableView-- 带有一些非严格方法的 Traversable。TraversableForwarder-前锋大多数方法underlying,除了toString,hashCode,equals,stringPrefix,newBuilder,view和所有调用创建相同类型的新的迭代对象。mutable.Traversable和immutable.Traversable- 与 相同Traversable,但限制集合类型。- 存在其他特殊情况
Iterable类,例如MetaData。 Iterable--Iterator可以为其创建 的集合(通过iterator)。IterableProxy、IterableView、mutable和immutable.Iterable。
Iterator- 不是 的后代的特征Traversable。定义next和hasNext。CountedIterator-- 一个Iterator定义count,它返回到目前为止看到的元素。BufferedIterator-- 定义head,它返回下一个元素而不消耗它。- 存在其他特殊情况
Iterator类,例如Source。
The Maps
地图
Map-- AnIterableofTuple2, which also provides methods for retrieving a value (the second element of the tuple) given a key (the first element of the tuple). ExtendsPartialFunctionas well.MapProxy-- AProxyfor aMap.DefaultMap-- A trait implementing some ofMap's abstract methods.SortedMap-- AMapwhose keys are sorted.immutable.SortMapimmutable.TreeMap-- A class implementingimmutable.SortedMap.
immutable.Mapimmutable.MapProxyimmutable.HashMap-- A class implementingimmutable.Mapthrough key hashing.immutable.IntMap-- A class implementingimmutable.Mapspecialized forIntkeys. Uses a tree based on the binary digits of the keys.immutable.ListMap-- A class implementingimmutable.Mapthrough lists.immutable.LongMap-- A class implementingimmutable.Mapspecialized forLongkeys. SeeIntMap.- There are additional classes optimized for an specific number of elements.
mutable.Mapmutable.HashMap-- A class implementingmutable.Mapthrough key hashing.mutable.ImmutableMapAdaptor-- A class implementing amutable.Mapfrom an existingimmutable.Map.mutable.LinkedHashMap-- ?mutable.ListMap-- A class implementingmutable.Mapthrough lists.mutable.MultiMap-- A class accepting more than one distinct value for each key.mutable.ObservableMap-- A mixinwhich, when mixed with aMap, publishes events to observers through aPublisherinterface.mutable.OpenHashMap-- A class based on an open hashing algorithm.mutable.SynchronizedMap-- A mixinwhich should be mixed with aMapto provide a version of it with synchronized methods.mutable.MapProxy.
Map-- 一个IterableofTuple2,它还提供了在给定键(元组的第一个元素)的情况下检索值(元组的第二个元素)的方法。也延伸PartialFunction。MapProxy- AProxy为Map.DefaultMap-- 实现某些Map抽象方法的特征。SortedMap-- AMap其键已排序。immutable.SortMapimmutable.TreeMap- 一个类实现immutable.SortedMap.
immutable.Mapimmutable.MapProxyimmutable.HashMap--immutable.Map通过密钥散列实现的类。immutable.IntMap--immutable.Map专门为Int键实现的类。使用基于密钥的二进制数字的树。immutable.ListMap--immutable.Map通过列表实现的类。immutable.LongMap--immutable.Map专门为Long键实现的类。见IntMap。- 还有针对特定数量的元素优化的其他类。
mutable.Mapmutable.HashMap--mutable.Map通过密钥散列实现的类。mutable.ImmutableMapAdaptor-mutable.Map从现有的immutable.Map.mutable.LinkedHashMap——?mutable.ListMap--mutable.Map通过列表实现的类。mutable.MultiMap-- 为每个键接受多个不同值的类。mutable.ObservableMap-- 一个mixin,当与 a 混合时Map,通过Publisher接口向观察者发布事件。mutable.OpenHashMap-- 基于开放散列算法的类。mutable.SynchronizedMap--应与 a 混合以提供具有同步方法的版本的mixinMap。mutable.MapProxy.
The Sequences
序列
Seq-- A sequence of elements. One assumes a well-defined size and element repetition. ExtendsPartialFunctionas well.IndexedSeq-- Sequences that support O(1) element access and O(1) length computation.IndexedSeqViewimmutable.PagedSeq-- An implementation ofIndexedSeqwhere the elements are produced on-demand by a function passed through the constructor.immutable.IndexedSeqimmutable.Range-- A delimited sequence of integers, closed on the lower end, open on the high end, and with a step.immutable.Range.Inclusive-- ARangeclosed on the high end as well.immutable.Range.ByOne-- ARangewhose step is 1.
immutable.NumericRange-- A more generic version ofRangewhich works with anyIntegral.immutable.NumericRange.Inclusive,immutable.NumericRange.Exclusive.immutable.WrappedString,immutable.RichString-- Wrappers which enables seeing aStringas aSeq[Char], while still preserving theStringmethods. I'm not sure what the difference between them is.
mutable.IndexedSeqmutable.GenericArray-- AnSeq-based array-like structure. Note that the "class"Arrayis Java'sArray, which is more of a memory storage method than a class.mutable.ResizableArray-- Internal class used by classes based on resizable arrays.mutable.PriorityQueue,mutable.SynchronizedPriorityQueue-- Classes implementing prioritized queues -- queues where the elements are dequeued according to anOrderingfirst, and order of queueing last.mutable.PriorityQueueProxy-- an abstractProxyfor aPriorityQueue.
LinearSeq-- A trait for linear sequences, with efficient time forisEmpty,headandtail.immutable.LinearSeqimmutable.List-- An immutable, singlely-linked, list implementation.immutable.Stream-- A lazy-list. Its elements are only computed on-demand, but memoized (kept in memory) afterwards. It can be theoretically infinite.
mutable.LinearSeqmutable.DoublyLinkedList-- A list with mutableprev,head(elem) andtail(next).mutable.LinkedList-- A list with mutablehead(elem) andtail(next).mutable.MutableList-- A class used internally to implement classes based on mutable lists.mutable.Queue,mutable.QueueProxy-- A data structure optimized for FIFO (First-In, First-Out) operations.mutable.QueueProxy-- AProxyfor amutable.Queue.
SeqProxy,SeqView,SeqForwarderimmutable.Seqimmutable.Queue-- A class implementing a FIFO-optimized (First-In, First-Out) data structure. There is no common superclass of bothmutableandimmutablequeues.immutable.Stack-- A class implementing a LIFO-optimized (Last-In, First-Out) data structure. There is no common superclass of bothmutableimmutablestacks.immutable.Vector-- ?scala.xml.NodeSeq-- A specialized XML class which extendsimmutable.Seq.immutable.IndexedSeq-- As seen above.immutable.LinearSeq-- As seen above.
mutable.ArrayStack-- A class implementing a LIFO-optimized data structure using arrays. Supposedly significantly faster than a normal stack.mutable.Stack,mutable.SynchronizedStack-- Classes implementing a LIFO-optimized data structure.mutable.StackProxy-- AProxyfor amutable.Stack..mutable.Seqmutable.Buffer-- Sequence of elements which can be changed by appending, prepending or inserting new members.mutable.ArrayBuffer-- An implementation of themutable.Bufferclass, with constant amortized time for the append, update and random access operations. It has some specialized subclasses, such asNodeBuffer.mutable.BufferProxy,mutable.SynchronizedBuffer.mutable.ListBuffer-- A buffer backed by a list. It provides constant time append and prepend, with most other operations being linear.mutable.ObservableBuffer-- A mixintrait which, when mixed to aBuffer, provides notification events through aPublisherinterfaces.mutable.IndexedSeq-- As seen above.mutable.LinearSeq-- As seen above.
Seq——元素序列。一种假设是明确定义的大小和元素重复。也延伸PartialFunction。IndexedSeq-- 支持 O(1) 元素访问和 O(1) 长度计算的序列。IndexedSeqViewimmutable.PagedSeq--IndexedSeq通过构造函数传递的函数按需生成元素的实现。immutable.IndexedSeqimmutable.Range-- 一个分隔的整数序列,在低端封闭,在高端开放,并带有一个台阶。immutable.Range.Inclusive-- 也Range关闭了高端。immutable.Range.ByOne--Range步长为 1 的A。
immutable.NumericRange- 一个更通用的版本Range,适用于任何Integral.immutable.NumericRange.Inclusive,immutable.NumericRange.Exclusive.immutable.WrappedString,immutable.RichString-- 可以将 aString视为 a 的包装器Seq[Char],同时仍保留String方法。我不确定它们之间的区别是什么。
mutable.IndexedSeqmutable.GenericArray-- 一个Seq基于数组的结构。请注意,“类”Array是 Java 的Array,它更像是一种内存存储方法,而不是一个类。mutable.ResizableArray-- 基于可调整大小的数组的类使用的内部类。mutable.PriorityQueue,mutable.SynchronizedPriorityQueue-- 实现优先队列的类 -- 元素根据Ordering第一个出队和最后入队的顺序的队列。mutable.PriorityQueueProxy-一个抽象Proxy的PriorityQueue。
LinearSeq-- 线性序列的特征,对于isEmpty,head和具有有效的时间tail。immutable.LinearSeqimmutable.List-- 一个不可变的、单链接的列表实现。immutable.Stream-- 一个懒惰的列表。它的元素仅按需计算,但随后被记忆(保存在内存中)。理论上可以是无限的。
mutable.LinearSeqmutable.DoublyLinkedList-- 一个包含可变prev,head(elem) 和tail(next) 的列表。mutable.LinkedList-- 具有可变head(elem) 和tail(next) 的列表。mutable.MutableList-- 内部用于实现基于可变列表的类的类。mutable.Queue,mutable.QueueProxy-- 针对 FIFO(先进先出)操作优化的数据结构。mutable.QueueProxy- AProxy为mutable.Queue.
SeqProxy,SeqView,SeqForwarderimmutable.Seqimmutable.Queue-- 一个实现 FIFO 优化(先进先出)数据结构的类。mutable和immutable队列没有共同的超类。immutable.Stack-- 实现 LIFO 优化(后进先出)数据结构的类。两个mutableimmutable堆栈没有共同的超类。immutable.Vector——?scala.xml.NodeSeq- 一个专门的 XML 类,它扩展了immutable.Seq.immutable.IndexedSeq——如上所见。immutable.LinearSeq——如上所见。
mutable.ArrayStack-- 使用数组实现 LIFO 优化数据结构的类。据说比普通堆栈快得多。mutable.Stack,mutable.SynchronizedStack-- 实现 LIFO 优化数据结构的类。mutable.StackProxy-- AProxy为mutable.Stack..mutable.Seqmutable.Buffer-- 可以通过附加、前置或插入新成员来更改的元素序列。mutable.ArrayBuffer--mutable.Buffer类的一个实现,对于追加、更新和随机访问操作具有固定的分摊时间。它有一些专门的子类,例如NodeBuffer.mutable.BufferProxy,mutable.SynchronizedBuffer.mutable.ListBuffer-- 由列表支持的缓冲区。它提供恒定时间追加和前置,大多数其他操作是线性的。mutable.ObservableBuffer-- 一个mixintrait,当与 a 混合时Buffer,通过Publisher接口提供通知事件。mutable.IndexedSeq——如上所见。mutable.LinearSeq——如上所见。
The Sets
套装
Set-- A set is a collection that includes at most one of any object.BitSet-- A set of integers stored as a bitset.immutable.BitSetmutable.BitSet
SortedSet-- A set whose elements are ordered.immutable.SortedSetimmutable.TreeSet-- An implementation of aSortedSetbased on a tree.
SetProxy-- AProxyfor aSet.immutable.Setimmutable.HashSet-- An implementation ofSetbased on element hashing.immutable.ListSet-- An implementation ofSetbased on lists.- Additional set classes exists to provide optimized implementions for sets from 0 to 4 elements.
immutable.SetProxy-- AProxyfor an immutableSet.
mutable.Setmutable.HashSet-- An implementation ofSetbased on element hashing.mutable.ImmutableSetAdaptor-- A class implementing a mutableSetfrom an immutableSet.LinkedHashSet-- An implementation ofSetbased on lists.ObservableSet-- A mixintrait which, when mixed with aSet, provides notification events through aPublisherinterface.SetProxy-- AProxyfor aSet.SynchronizedSet-- A mixintrait which, when mixed with aSet, provides notification events through aPublisherinterface.
Set-- 集合是最多包含任何对象中的一个的集合。BitSet-- 一组存储为位集的整数。immutable.BitSetmutable.BitSet
SortedSet-- 元素有序的集合。immutable.SortedSetimmutable.TreeSet-- 一个SortedSet基于树的实现。
SetProxy- AProxy为Set.immutable.Setimmutable.HashSet--Set基于元素散列的实现。immutable.ListSet--Set基于列表的实现。- 存在额外的集合类来为从 0 到 4 个元素的集合提供优化的实现。
immutable.SetProxy-Proxy一个不可变的Set。
mutable.Setmutable.HashSet--Set基于元素散列的实现。mutable.ImmutableSetAdaptor-Set从不可变的实现可变的类Set。LinkedHashSet--Set基于列表的实现。ObservableSet-- 一个mixintrait,当与 a 混合时Set,通过Publisher接口提供通知事件。SetProxy- AProxy为Set.SynchronizedSet-- 一个mixintrait,当与 a 混合时Set,通过Publisher接口提供通知事件。
- Why the Like classes exist (e.g. TraversableLike)
- 为什么 Like 类存在(例如 TraversableLike)
This was done to achieve maximum code reuse. The concrete genericimplementation for classes with a certain structure (a traversable, a map, etc) is done in the Like classes. The classes intended for general consumption, then, override selected methods that can be optmized.
这样做是为了实现最大的代码重用。具有特定结构(可遍历、映射等)的类的具体通用实现在 Like 类中完成。然后,用于一般消费的类会覆盖可以优化的选定方法。
- What the companion methods are for (e.g. List.companion)
- 伴随方法的用途(例如 List.companion)
The builder for the classes, ie, the object which knows how to create instances of that class in a way that can be used by methods like map, is created by a method in the companion object. So, in order to build an object of type X, I need to get that builder from the companion object of X. Unfortunately, there is no way, in Scala, to get from class X to object X. Because of that, there is a method defined in each instance of X, companion, which returns the companion object of class X.
类的构建器,即知道如何以一种可以被诸如 之类的方法使用的方式创建该类的实例的对象map,是由伴随对象中的一个方法创建的。因此,为了构建类型 X 的对象,我需要从 X 的伴随对象中获取该构建器。不幸的是,在 Scala 中无法从类 X 获取对象 X。因此,有在 X, 的每个实例中定义的方法companion,它返回类 X 的伴随对象。
While there might be some use for such method in normal programs, its target is enabling code reuse in the collection library.
虽然在普通程序中可能会使用这种方法,但它的目标是在集合库中实现代码重用。
- How I know what implicit objects are in scope at a given point
- 我如何知道在给定点的范围内有哪些隐式对象
You aren't supposed to care about that. They are implicit precisely so that you don't need to figure out how to make it work.
你不应该关心那个。它们是隐式的,因此您无需弄清楚如何使其工作。
These implicits exists to enable the methods on the collections to be defined on parent classes but still return a collection of the same type. For example, the mapmethod is defined on TraversableLike, but if you used on a Listyou'll get a Listback.
这些隐式的存在是为了使集合上的方法能够在父类上定义,但仍然返回相同类型的集合。例如,该map方法是在 上定义的TraversableLike,但是如果您在 a 上使用,List您将得到List回报。

