Why initialize HashSet<>(0) to zero?

前端 未结 5 1211
半阙折子戏
半阙折子戏 2021-02-07 02:50

I love a HashSet<>() and use this eagerly while initializing this with the default constructor:

Set users = new HashSet<>();
         


        
5条回答
  •  不要未来只要你来
    2021-02-07 03:51

    This will set it to the minimum.

    Most likely this is used to turn off code analysers which can complain if you haven't set an initial capacity for collections. By setting it to 0 you just set it to the minimum.

    It is not much of an optimisation because as soon as you add an entry, the load factor of 0.7 will make the capacity 2, recreating the Map.Entry[] in the process.

提交回复
热议问题