How to use trove for multi hashmap with byte array as key?

603 views Asked by At

currently I'm working with an ArrayListMultiMap of Guave where I handle over 100.000 items. The key of the map is a byte array, the values are long.

Now I want to reduce the overhead.

My idea is to use only primitive collections and the hashmap of trove. So in the end each key (byte array) points to a primitive collection (primitive long set).

My question is how to use a byte array as key in a THashmap. In Guave I wrapped the byte array in a class, but this produces overhead.

thank you

2

There are 2 answers

2
mfulton26 On

You can use a byte array as the key directly (no need to wrap it, etc.):

Map<byte[], TLongSet> map = new THashMap<>();
map.put(new byte[]{-0x80, 0x7F}, new TLongHashSet(new long[]{0xFFFFFFFF, 0x7FFFFFFF}));
System.out.println(map);

Example output:

{[B@4d7e1886={2147483647,-1}}

EDIT:

If you do not want identity-based keys then you can use TByteArrayList instead of byte[]. On my machine it has an overhead of 25 bytes per instance. If each of your 100,000 items/longs were stored in its own TLongSet each mapped to a unique byte array then this makes an overhead of 2.5 megabytes (25 bytes x 100,000). This seems acceptable to me as a worse-case-scenario but if you do want to avoid it then Rob Eden's answer with a TCustomHashMap seems the way to go.

1
Rob Eden On

You should use TCustomHashMap for the outer map because you'll want to come up with a hashing strategy for the byte[] (really, just one that calls Arrays.hashCode(byte[]) would be good).

Within the outer map, use a TLongHashSet (or whatever) for values and you're all set.