Open
Description
It is somewhat unfortunate that size
is O(n) for HashMap
and HashSet
:
-
If a faster
size
is desired, I have to store it separately in my code and make sure that I update it whenever I do aninsert
ordelete
. With filtering functions,union
, etc. it gets even more cumbersome. -
Even experienced programmers often assume that
size
is O(1) without checking, which leads to performance bugs :(
(I have looked at the code, but I have to confess that I don't understand how HashMap/Set
work and so it's hard for me to assess how much of a performance/memory penalty adding a size
field to the constructors would be, or whether it is possible at all.)