Java Hash Table Issue with Object Reference

怎甘沉沦 提交于 2019-12-10 20:39:58

问题


I have a Hash Table like,

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

Now, I implement it like, Integer foo = Integer(1) and declare hash table like,

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

Now, as per I understood from this, it will reduce heap space used by JVM. Is this correct ? Another point is that, in C, I usually use structure like,

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

And then use the location to access the value. Now, if mem value is less than Int (say Byte), I can save space. However, I don't understand how to implement this in Java. Or is there any other way to reduce space of the hash table ? (means by reducing repeatedly storing same object in Java's way).


回答1:


The most space-efficient way with Integers is to use Integer.valueOf(), which uses the flyweight design pattern to reduce the memory usage for small values. Values between -128 and (usually) 127 need no additional memory.



来源:https://stackoverflow.com/questions/9661374/java-hash-table-issue-with-object-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!