为什么插入 ListMap 的时间复杂度为 O(n)?

Why are insertions into ListMap O(n)?

根据 scala 文档,insertions into the ListMap are O(n)

Other operations, such as inserting or removing entries, are also O(n), which makes this collection suitable only for a small number of elements.

插入链表头部不应该是O(1)吗?为什么插入是O(n)?

当您向非空 ListMap 添加元素时,会调用 ListMap.Nodeupdated 方法。在最坏的情况下,它会扫描整个集合以检查密钥是否已经存在(正如您在评论中所建议的那样): https://github.com/scala/scala/blob/8a2cf63ee5bad8c8c054f76464de0e10226516a0/src/library/scala/collection/immutable/ListMap.scala#L173