RFR: 8186958: Need method to create pre-sized HashMap [v12]
XenoAmess
duke at openjdk.java.net
Sat Apr 9 16:33:33 UTC 2022
On Sat, 9 Apr 2022 16:02:17 GMT, liach <duke at openjdk.java.net> wrote:
> > > Quick question: If the maps are intended to be fixed-size, can't the users just call `new HashMap<>(size, 1)`, increasing the growth factor to prevent growth?
> >
> >
> > @liach this questions equals question : "why default load factor be 0.75 not 1" In short, when larger the load factor, smaller memory use, and larger hash-collide rate, larger crud time cost. when smaller the load factor, larger memory use, and smaller hash-collide rate, smaller crud time cost. As for why default load factor be 0.75 but not 1, it is historical to me. it is already 0.75 when I first learn java and never changed then. But I guess there be some empirical formula behind it.
>
> No. I mean when the growth factor is 1 and exactly `size` key-value pairs are put into the map, the map will not grow; but when it's 0.75, the map will preemptively grow erroneously. Changing the growth factor is effectively no-op for a hash map that will not have more elements added later during its usage.
I don't think you get what I mean.
For example.
There be a people who want a 16-elements map.
If use `new HashMap(16 , 1)`, then he will get a table-length-16 hashMap. It will not growth, wth larger load factor, smaller memory use, and larger hash-collide rate, larger crud time cost.
if use `new HashMap(22, 0.75)`, (` 22 = Math.celil(16 / 0.75)`)then he will get a table-length-32 hashMap. It will not growth, with smaller load factor, larger memory use, and smaller hash-collide rate, smaller crud time cost.
I hope the example can help you understand what is load factor's usage, and be clear about the difference between using 0.75 and 1 as load factor.
-------------
PR: https://git.openjdk.java.net/jdk/pull/7928
More information about the core-libs-dev
mailing list