HashMap initialization parameters (load / initialcapacity)

前端 未结 9 1477
[愿得一人]
[愿得一人] 2020-12-04 10:54

What values should I pass to create an efficient HashMap / HashMap based structures for N items?

In an ArrayList, the efficien

9条回答
  •  失恋的感觉
    2020-12-04 11:14

    It's safe in most cases of List and Map initialization to make the List or Map with the following size params.

    List(numElements + (numElements / 2));
    Map(numElements + (numElements / 2));
    

    this follows the .75 rule as well as saves a little overhead over the * 2 operation described above.

提交回复
热议问题