Time complexity of Map.clear in Scala
When called on a Map, is the time complexity of clear O(1) or O(n) on the size of the map?
In the case of immutable maps don't have clear since they are immutable but they do have empty which returns a new empty map of that type. Since it is just an object creation it is O(1). Mutable maps however are O(n) where n is based on the underlying data structure since they have to clear out that data structure. For the typical mutable HashMap this will be a table. The table size will be between 0.75 and 1.5 the maximum size the Map has been in the past.