带有对象引用的Java哈希表问题 [英] Java Hash Table Issue with Object Refference

查看:107
本文介绍了带有对象引用的Java哈希表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个哈希表,如

$ 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 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.

这篇关于带有对象引用的Java哈希表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆