[英]Memory-efficient sparse array in Java
(There are some questions about time-efficient sparse arrays but I am looking for memory efficiency.) (关于时间有效的稀疏数组有一些问题,但我正在寻找内存效率。)
I need the equivalent of a List<T>
or Map<Integer,T>
which 我需要等效的
List<T>
或Map<Integer,T>
ArrayList<T>
in the case that most of the indices are not null
, ie when the actual data is not very sparse. null
的情况下,即当实际数据不是非常稀疏时,与ArrayList<T>
一样具有内存效率。 null
indices. null
索引的数量成比例的空间。 HashMap<Integer,T>
(as this autoboxes the keys and probably does not take advantage of the scalar key type). HashMap<Integer,T>
更少的内存(因为这会自动锁定密钥并且可能不利用标量密钥类型)。 Does anyone know of such a utility class? 有谁知道这样的实用类?
I would have expected Commons Collections to have one but it did not seem to. 我本来期望Commons Collections有一个,但它似乎没有。
I came across org.apache.commons.math.util.OpenIntToFieldHashMap
which looks almost right except the value type is a FieldElement
which seems gratuitous; 我遇到了
org.apache.commons.math.util.OpenIntToFieldHashMap
,看起来几乎正确,除了值类型是一个看似无偿的FieldElement
; I just want T extends Object
. 我只想要
T extends Object
。 It looks like it would be easy to edit its source code to be more generic, though I would rather use a binary dependency if one is available. 它看起来很容易编辑它的源代码更通用,但我宁愿使用二进制依赖,如果有一个可用。
我会尝试用宝库收藏,有TIntObjectMap它可以为您的意图工作。
I would look at Android's SparseArray implementation for inspiration. 我会看看Android的SparseArray实现的灵感。 You can view the source by downloading AOSP's source code here http://source.android.com/source/downloading.html
您可以在http://source.android.com/source/downloading.html上下载AOSP的源代码来查看源代码
I have saved my test case as jglick/inthashmap . 我已将测试用例保存为jglick / inthashmap 。 The results:
结果:
HashMap size: 1017504
TIntObjectMap size: 853216
IntHashMap size: 846984
OpenIntObjectHashMap size: 760472
Late to this question, but there is IntMap in libgdx which uses cuckoo hashing . 在这个问题的后期,但是libgdx中的IntMap使用了布谷鸟散列 。 If anything it would be interesting to compare with the others.
如果有的话,与其他人比较会很有趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.