Search code examples
javagenericsautoboxingfastutilkoloboke

Need an optimised Map(k,v) e.g. (long, long[]), to avoid auto boxing


I have a piece of code which is basically as followed:

long[]   ids;
long[][] values;

The values are filled out of turn i.e. if ids = ['id1','id2',...] the values maybe be
values = [['id2val1','id2val2',..]['id1val1','id2val2',...],..]
The out of turn execution cannot be avoided, the approach I am considering is to use a Map Map<Long,Long[]>,
but due to auto boxing of java it is not possible use the JAVA generics.

I would like to know if it can be done via some optimised data structure library that uses primitives,
to avoid the unnecessary auto boxing, I am looking at libraries like Koloboke & Fastutil.
Looking for a data structure recommendation


Solution

  • While you have to use boxed Long as the map key, you do not need to box the primitives in value arrays. You can use Map<Long,long[]> rather than Map<Long,Long[]>. This should alleviate most of your performance concerns.

    So, before you start introducing third party libraries, see if simple HashMap<Long,long[]> will be good enough for your purposes.