a little primitive question
I am looking to create a key-value pairs (map, table, etc) where the
key should be a primitive data type like int, long ,etc. My keys can
have high numbers
Hashmap, hashtable and all the other collections need Object as key
I can use array but because high key numbers it won't be memory
efficient
e.g: if i have a key of 10005 then the array has to be of the minimum
size 10005 so that i can store my value at array[10005] and later
access it directly
any thoughts?
Student
Daniel Pitts - 16 Nov 2007 01:10 GMT
> a little primitive question
>
[quoted text clipped - 13 lines]
>
> Student
You can use a primitive wrapper class as the key
Map<Integer, String> map = new HashMap<Integer, String>();
With auto-boxing, you can do stuff like:
map.put(10, "Hello");
map.put(20, "Go to 10");
Alternatively, you can create your own implementation of a primitive
keyed map, but I wouldn't suggest it.

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>