HashMap initialization parameters (load / initialcapacity)

前端 未结 9 1485
[愿得一人]
[愿得一人] 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:28

    In the guava libraries from Google there is a function that creates a HashMap optimized for a expected number of items: newHashMapWithExpectedSize

    from the docs:

    Creates a HashMap instance, with a high enough "initial capacity" that it should hold expectedSize elements without growth ...

提交回复
热议问题