Why initialize HashSet<>(0) to zero?

前端 未结 5 1152
半阙折子戏
半阙折子戏 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:44

    If you see docs

    There is some clue.

    Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

提交回复
热议问题