Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no mutable TreeMap in Scala?

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

like image 680
soc Avatar asked Dec 25 '10 22:12

soc


People also ask

Is TreeMap mutable?

Companion object TreeMapA mutable sorted map implemented using a mutable red-black tree as underlying data structure.

What is TreeMap in Scala?

Companion object TreeMapAn immutable SortedMap whose values are stored in a red-black tree. This class is optimal when range queries will be performed, or when traversal in order of an ordering is desired.

Is a TreeMap automatically sorted?

2. Default Sorting in TreeMap. By default, TreeMap sorts all its entries according to their natural ordering. For an integer, this would mean ascending order and for strings, alphabetical order.

Does TreeMap maintains ascending order?

A TreeMap is a Map that maintains its entries in ascending order, sorted according to the keys' natural ordering, or according to a Comparator provided at the time of the TreeMap constructor argument. The TreeMap class is efficient for traversing the keys in a sorted order.


2 Answers

It's just a missing case that will presumably eventually be filled in. There is no reason not to do it, and in certain cases it would be considerably faster than the immutable tree (since modifications require log(n) object creations with an immutable tree and only 1 with a mutable tree).


Edit: and in fact it was filled in in 2.12.

Mutable TreeMap.

(There is a corresponding Set also.)

like image 50
Rex Kerr Avatar answered Sep 24 '22 20:09

Rex Kerr


Meanwhile you can use the Java TreeMap, which is exactly what you need.

val m = new java.util.TreeMap[String, Int]() m.put("aa", 2) m.put("cc", 3) 
like image 37
Adrian Avatar answered Sep 26 '22 20:09

Adrian