Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
ListBuffers 的文档提供了两种将 ListBuffer 转换为 List 的方法:result和toList.
result
toList
result说它从添加的元素中生成一个集合,并且之后的内容是未定义的。
toList似乎改为对缓冲区内容进行恒定时间的惰性副本(并且可能使缓冲区保持不变)。
如果toList是恒定时间,我们会更喜欢什么时候result?而且我是否正确理解这toList将保留缓冲区的内容?
如果我们查看源代码,我们会看到
def result: List[A] = toList
所以(至少在当前版本的 Scala 中),没有必要偏爱一个。