0

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

Community
  • 1
  • 1
501 - not implemented
  • 2,638
  • 4
  • 39
  • 74

2 Answers2

4

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.

Rob Eden
  • 362
  • 2
  • 7
2

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.

Community
  • 1
  • 1
mfulton26
  • 29,956
  • 6
  • 64
  • 88
  • This won't work as-is because you won't be able to search for keys since equals/hashCode won't work for arrays the way you would want them to. (They're identity-based, not value-based.) You'll want to use TCustomHashMap with a HashingStrategy instead. – Rob Eden Aug 25 '16 at 14:11
  • @RobEden, good point. The questioner did not specify why they were wrapping the byte array but that is likely why. Thank you for pointing that out. – mfulton26 Aug 25 '16 at 14:38