key-value pair , with key as primitive data type

S

studentoflife77

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
 
D

Daniel Pitts

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
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top