What is the best map implementation to use when storing integers with char keys?

I have a set of flags which are part of a huge text data file as single characters. Before processing the file I map each flag to the id of a property it represents. While processing the file I need to look up these mappings as fast as possible (I do it a lot).

Currently I store these in a HashMap. And the code looks like this:

    private HashMap<Integer, Integer> _propertyKeys;

    private int _getKeyedProperty(char key) {
      return (_propertyKeys.get((int) key));
    }

Is there any way I could be doing this faster, using a better implementation of Map than HashMap or even using arrays to prevent boxing/unboxing?

Answer

You could use TIntIntHashMap from GNU Trove. It uses primitives for the keys and values.

I have used the GNU Trove primitive list classes and found that they give a noticeable performance improvement when compared to the standard list classes using autoboxing for primitives.

Leave a Reply

Your email address will not be published. Required fields are marked *