In most cases, where the Strings inside your array are not pathological and do not include commas followed by a space, you can use Arrays.toString()
as a unique key. i.e. your Map
would be a Map<String, T>
. And the get/put for an array myKeys[]
would be
T t = myMap.get(Arrays.toString(myKeys));
myMap.put(Arrays.toString(myKeys), myT);
Obviously you could put in some wrapper code if desired.
A nice side effect is that your key is now immutable. Of course, of you change your array myKeys and then try a get()
, you won't find it.
Hashing of Strings is highly optimized. So my guess is that this solution, though it feels a bit slow and kludgy, will be both faster and more memory efficient (less object allocations) than @Ted Hopp solution using an immutable List. Just think about whether Arrays.toString()
is unique for your keys. If not, or if there is any doubt, (e.g. the String[] comes from user input) use the List.
hashCode()
to determine the hash of an object. – Mural