I have a problem where i need to store key, value1(float) and value2 (very long string) in a map, where value1 is available for every key and value2 is available only for 1% of the keys.
I can think of 2 possible solution
two maps like map1 = map(key1 ,map(key2, value1)) and map2 = map(key1, map(key2,value2)) pros - no unnecessary reference variables. cons - storing the same key twice wasting memory there.
use one map with a custom object value. map1 = map customobj{float value1; string value2} pros - no duplication of key. cons- 99% of the customobj will have value2=null and hence will consume memory for reference pointer.
Basically my ultimate question is does unused references (in customobj) consume memory or would compiler optimize it ? i am leaning towards soln 2, as i dont want to waste memory by storing the same key1 and key2 twice. On the other hand 99% of the time value2=null, which makes me wonder whether soln1 is better.
I am using Java and I would like to hear some advice.
EDIT: I didnt realize that SO didnt print the map structure i posted, i edited that . both key1 and key2 are string (mostly fixed length id string)