Java哈希表问题与对象参考 [英] Java Hash Table Issue with Object Reference
问题描述
我有一个哈希表,
HashTable ht = { (1, 1), (2, 1), (3, 1) }
现在,我像Integer foo = Integer(1)那样实现它,并像这样声明哈希表,
Now, I implement it like, Integer foo = Integer(1) and declare hash table like,
HashTable ht = { (foo, foo), (2, foo), (3, foo) }
现在,据我从此了解,这将减少JVM使用的堆空间.这是正确的吗?另一点是,在C语言中,我通常使用类似这样的结构,
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 }
然后使用该位置访问值.现在,如果mem值小于Int(例如Byte),则可以节省空间.但是,我不明白如何在Java中实现这一点. 或者还有其他减少哈希表空间的方法吗? (通过减少以Java的方式重复存储同一对象的方法).
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).
推荐答案
使用Integer.valueOf()
的最节省空间的方法是使用Integer.valueOf()
,它使用flyweight设计模式来减少较小值的内存使用量.介于-128和(通常)127之间的值不需要额外的内存.
The most space-efficient way with Integer
s 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.
这篇关于Java哈希表问题与对象参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!