在scala集合库中Buffer
继承自Seq
:
Buffer[A] extends Seq[A] with GenericTraversableTemplate[A, Buffer] with BufferLike[A, Buffer[A]] with scala.Cloneable
并且 Buffer
文档说:
Buffers are used to create sequences of elements incrementally by appending, prepending, or inserting new elements. It is also possible to access and modify elements in a random access fashion via the index of the element in the current sequence.
而IndexedSeq
文档说:
A base trait for indexed sequences.
Indexed sequences support constant-time or near constant-time element access and length computation. They are defined in terms of abstract methods apply for indexing and length.
Indexed sequences do not add any new methods to Seq, but promise efficient implementations of random access patterns.
由于 Buffer
已经扩展了 Seq
并且 IndexedSeq
没有向 Seq
添加任何方法
Buffer 必须已经实现了 IndexedSeq 接口(interface),并且根据文档
它应该满足IndexedSeq
的非功能性要求。
那么为什么 Buffer
不是 IndexedSeq
。
最佳答案
Buffer
不是 IndexedSeq
,因为它不保证接近恒定时间的元素访问和长度计算。例如,ListBuffer
都不支持,正如您在 description of the performance characteristics of Scala collections 中看到的那样。 .
关于scala - 为什么 Buffer 不是 IndexedSeq 的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15142708/