In my program key-value pairs are frequently added to a Map
until 1G of pairs are added. Map resizing slows down the process. How can I set minimum Map
size to, for example 1000000007 (which is a prime)?

- 1,391
- 18
- 40
-
7Use the constructor that takes the initial size? – Dave Newton Nov 08 '16 at 16:27
-
Will the constructor prevent map from resizing down? – Stepan Nov 08 '16 at 16:27
-
I'm voting to close this question as off-topic because the OP did not do any research at all. There is a constructor for exactly that purpose. – f1sh Nov 08 '16 at 16:29
-
@Stepan, Why don't you just read the documentation about how `initialCapacity` is used? – Jaroslaw Pawlak Nov 08 '16 at 16:29
-
HashMap should have a size that is prime to minimize clustering. – Stepan Nov 08 '16 at 16:29
-
@Stepan http://stackoverflow.com/a/15437377/438992 – Dave Newton Nov 08 '16 at 16:30
-
Well, I got the answer. Gotta delete the question now. Thanks for all downvotes. Answerer's efford is wasted though. – Stepan Nov 08 '16 at 16:32
-
1@Stepan Then don't delete it--I didn't downvote, but I mean, come on, it's right there in the docs :/ – Dave Newton Nov 08 '16 at 16:48
3 Answers
The constructor of a HashMap
takes the initial size of the map (and the load factor, if desired).
Map<K,V> map = new HashMap<>(1_000_000_007);

- 8,526
- 1
- 25
- 44
-
1See [HashMap Constructor](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#HashMap-int-) for additional info, if this answer isn't clear enough. – Cache Staheli Nov 08 '16 at 16:28
-
1
How can I set minimum Map size to, for example 1000000007 (which is a prime)?
Using the HashMap(int)
or HashMap(int, float)
constructor. The int
parameter is the capacity.
HashMap should have a size that is prime to minimize clustering.
Past and current implementations of the HashMap
constructor will all choose a capacity that is the smallest power of 2 (up to 230) which is greater or equal to the supplied capacity. So using a prime number has no effect.
Will the constructor prevent map from resizing down?
HashMaps don't resize down.
(Note that size and capacity are different things. The size()
method returns the number of currently entries in the Map
. You can't "set" the size.)

- 698,415
- 94
- 811
- 1,216
-
Actually, this was his question, not how to create a map with an initial size. – ROMANIA_engineer Nov 08 '16 at 16:30
-
@ROMANIA No, the initial question was how to create a map with a minimum size. It's right there in the question. – Dave Newton Nov 08 '16 at 16:31
-
@DaveNewton , it's debatable, but he wanted (according to the title) to **Set minimum size of a Map in Java**, not **Set initial size of a Map in Java**. This is what I understand - that he believed that the `HashMap` will reduce its size during some actions and he wanted to fix a minimum size. – ROMANIA_engineer Nov 08 '16 at 16:34
A could of things you should note. The number of buckets in a HashMap is a power of 2 (might not be in future), the next power of 2 is 2^30. The load factor determines at what size it should grow the Map. Typically this is 0.75.
If you set the capacity to be the expected size, it will;
- round up to the next power of 2
- might still resize when the capacity * 0.75 is reached.
- is limited to 2^30 anyway as it is the largest power of 2 you can have for the size of an array.
Will the constructor prevent map from resizing down?
The only way to do this is to copy all the elements into a new Map. This is not done automatically.

- 525,659
- 79
- 751
- 1,130
-
1Actually, the number of buckets in **_the current implementation_** of `HashMap` is a power of 2. It probably will not change, but that implementation detail is *not* part of the interface contract and shouldn't be relied on. It may not always be a power of 2. – AJNeufeld Nov 08 '16 at 16:43