Memory Improvement: IntegerHashMap

We use HashMap<Integer, SomeType>()with over a million entries. I find that big.

But integers are their own hash code. Could you save memory with, say, IntegerHashMap<Integer, SomeType>()that uses a special one Map.Entry, using int directly instead of a pointer to an object Integer? In our case, this will save the 1000000x memory needed for the object Integer.

Any errors in my mind? Too special for general interest? (at least exists EnumHashMap)

add1. The first general parameter is IntegerHashMapused to make it close to other implementations Map. Of course, this can be dropped.

add2. The same should be possible with other cards and collections. For example ToIntegerHashMap<KeyType, Integer>, IntegerHashSet<Integer>etc.

+4
source share
2 answers

What you are looking for is a Primitive Collection library. They are usually much better with memory and performance. One of the oldest / most popular libraries was called "Trove". However, it is a bit outdated. Currently, the main active libraries are:

See tests here

+2
source

A few words of caution:

+1

Source: https://habr.com/ru/post/1673313/


All Articles