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 an insert or delete. 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.)
It is somewhat unfortunate that
sizeis O(n) forHashMapandHashSet:If a faster
sizeis desired, I have to store it separately in my code and make sure that I update it whenever I do aninsertordelete. With filtering functions,union, etc. it gets even more cumbersome.Even experienced programmers often assume that
sizeis 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/Setwork and so it's hard for me to assess how much of a performance/memory penalty adding asizefield to the constructors would be, or whether it is possible at all.)