Search code examples
javahashmapmapslong-integer

Map with long as key in Java


My profiler state I spend an important part of the time wrapping long into Long which I want to avoid. I currently do it because I use Map<Long, V>. Is there an usual way to create a Map whose keys are long ?

I know it won't be a Map<long, V> because the type system does not allow it, but that does not mean that a map from long to V can't exists. Actually, it would be quite easy to do, since I would just need to copy the code of HashMap<K,V>, replace K by long, and do all changes required by the type system (E.g. iteration over key would not be an Iterator<K> anymore)

My question is does such a library already exists, or should I do it myself ?

The context is that my profiler state that, when doing batch processing, a third of the time is spent transforming long into Long, and there is really no reason to accept to pay this price for the type system


Solution

  • http://fastutil.di.unimi.it/ is a class of library doing exactly what I was asking for here