Why is there no mutable TreeMap in Scala?

后端 未结 5 1019
-上瘾入骨i
-上瘾入骨i 2020-12-29 19:17

Is it lack of time, some technical problem or is there a reason why it should not exist?

5条回答
  •  渐次进展
    2020-12-29 19:31

    You'll also notice that TreeSet doesn't have a mutable equivalent either. It's because they share the common base class RedBlack, and the underlying data structure that keeps the Trees ordered by elements or keys is a red-black tree. I don't know too much about this data structure, but it's pretty complex (insertion and removal are pretty expensive compared to other Maps), so I assume that had something to do with a mutable variant not being included.

    Basically, it's probably because the underlying data structure isn't readily mutable so TreeMap isn't. So, to answer your question, it's a technical problem. It can definitely be done though, there's just not much of a use case for it.

提交回复
热议问题