简体   繁体   English

Java中的内存高效稀疏数组

[英]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>

  1. Can grow on demand just by setting a key larger than any encountered before. 可以通过设置比以前遇到的任何键更大的键来按需增长。 (Can assume keys are nonnegative.) (可以假设键是非负的。)
  2. Is about as memory-efficient as an 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>一样具有内存效率。
  3. When the indices are sparse, consumes space proportional to the number of non- null indices. 当索引稀疏时,消耗与非null索引的数量成比例的空间。
  4. Uses less memory than HashMap<Integer,T> (as this autoboxes the keys and probably does not take advantage of the scalar key type). 使用比HashMap<Integer,T>更少的内存(因为这会自动锁定密钥并且可能不利用标量密钥类型)。
  5. Can get or set an element in amortized log(N) time where N is the number of entries: need not be linear time, binary search would be acceptable. 可以在分摊日志(N)时间内获取或设置元素,其中N是条目数:不必是线性时间,二元搜索是可接受的。
  6. Implemented in a nonviral open-source pure Java library (preferably in Maven Central). 在非病毒开源纯Java库中实现(最好在Maven Central中)。

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

I will suggest you to use OpenIntObjectHashMap from Colt library. 我建议你使用Colt库中的OpenIntObjectHashMap。 Link 链接

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM