带有对象引用的Java哈希表问题 [英] Java Hash Table Issue with Object Refference
问题描述
我有一个哈希表,如
$ p $ HashTable ht = {(1,1),(2,1), (3,1)}
现在,我实现它,如Integer foo = Integer(1)和声明散列表就像
HashTable ht = {(foo,foo),(2,foo),(3,foo) }
现在,根据我对这个,它会减少JVM使用的堆空间。这是正确的吗?另一点是,在C中,我通常使用像
HashTable ht = { (1,mem),(2,mem),(3,mem)}
{mem是内存位置(比如说10)1)
然后使用该位置访问该值。现在,如果mem值小于Int(比如Byte),我可以节省空间。但是,我不明白如何在Java中实现这一点。还是有其他减少散列表空间的方法吗? (意思是通过减少以Java的方式重复存储相同的对象)。任何想法?在此先感谢。
Integer的空间效率最高的方法
s是使用 Integer.valueOf()
,它使用flyweight设计模式来减少小值的内存使用量。 -128和(通常)127之间的值不需要额外的内存。 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). Any idea ? Thanks in advance.
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屋!