Does anybody know a HashMap-Implementation with int-keys?

Hi there,

I've a efficiency problem with HashMap's and Autoboxing. The HashMap I am using is a:

HashMap<Integer, Object> In fact the Object is just a dummy object, I just need to check wether a particular key is contained or not.

However with millions of entries this table eats up a lot of memory because the key is wrapped into an Integer (2 words overhead) and HashMap allocates HashMapEntries, which are quite large in heap.

If somebody would know a better way to check wether an int-key was already seen before I would be really happy! I guess a HashMap which uses native primitive ints as key and does not generate HashMapEntries to store its objects would be perfect?

Does anybody know such a beast? I already searched at Commons Collections but did not find something useful :-/

Thank you in advance, lg Clemens

[870 byte] By [linuxhippya] at [2007-11-26 16:09:26]
# 1
http://jakarta.apache.org/commons/lang/xref/org/apache/commons/lang/IntHashMap.htmlGot it from googling: [url http://www.google.com/search?q=IntHashMap]IntHashMap[/url]
Caffeine0001a at 2007-7-8 22:31:45 > top of Java-index,Core,Core APIs...
# 2
It's not a direct answer to your question, but the data structure you are describing is a 'set'. You should really be using a set, rather than hacking a hashmap the way you are. (As an aside, HashSet is implemented exactly as you describe)
dannyyatesa at 2007-7-8 22:31:45 > top of Java-index,Core,Core APIs...
# 3
Alternatively, you might want to consider a BitSet implementation (java.util.BitSet).
Herko_ter_Horsta at 2007-7-8 22:31:45 > top of Java-index,Core,Core APIs...
# 4
Thanks for all the suggestions. Because I started working on it after I read the first post I hacked the IntHashMap which works fine now - any only has 12byte overhead per entry :-)Thanks for all the suggestions, lg Clemens
linuxhippya at 2007-7-8 22:31:45 > top of Java-index,Core,Core APIs...
# 5

> Thanks for all the suggestions.

>

> Because I started working on it after I read the

> first post I hacked the IntHashMap which works fine

> now - any only has 12byte overhead per entry :-)

>

You should have read on!

I definitely recommend what Herko_ter_Horst suggested: BitSet is what you need. It fufills your requirements and requires only on Bit per entry instead of 12Byte. (Plus some constant overhead that does not depend on the number of records to store).

The only reason i can consider for using a regular Set over a BitSet are if the ints to store are obscenely large and scattered sparsely.

If they are are not scattered sparsely but the minimum value is quite large (but known), subtract the minimum value before storing.

Horst_Ma at 2007-7-8 22:31:45 > top of Java-index,Core,Core APIs...