Java hash table problem with object reference

I have a hash table like,

HashTable ht = { (1, 1), (2, 1), (3, 1) }

      

Now I am implementing this as Integer foo = Integer (1) and declaring the hash table as,

HashTable ht = { (foo, foo), (2, foo), (3, foo) }

      

Now, as I understood from this , this will reduce the heap space used by the JVM. It is right? Another thing is that in C I usually use a structure like,

HashTable ht = { (1, mem), (2, mem), (3, mem) } 
{ where mem is memory location (say 10) of 1 }

      

And then use location to access the value. Now if the mem value is less than Int (say in bytes) I can save space. However, I don't understand how to implement this in Java. Or is there some other way to reduce the space of the hash table? (means shorthand for multiple storage of the same object in Java).

+3


source to share


1 answer


The most space efficient c Integer

is the use Integer.valueOf()

that uses the flyweight design pattern to reduce memory usage for small values. Values ​​between -128 and (usually) 127 do not need additional memory.



+1


source







All Articles