HashMap/ArrayMap

来源:互联网 发布:程序员职业规划之道pdf 编辑:程序博客网 时间:2024/06/05 16:58

ArrayMap

ArrayMap is a generic key->value mapping data structure that is designed to bemore memory efficient than a traditional HashMap, this implementation is a version of the platform's ArrayMap that can be used on older versions of the platform. It keeps its mappings in an array data structure --an integer array of hash codes for each item, andan Object array of the key/value pairs. This allows it toavoid having to create an extra object for every entry put in to the map, and it also tries to control the growth of the size of these arrays more aggressively (since growing them only requires copying the entries in the array, not rebuilding a hash map).


If you don't need the standard Java container APIs provided here (iterators etc), consider using SimpleArrayMap instead.


Note that this implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.


Because this container is intended to better balance memory use, unlike most other standard Java containers it will shrink its array as items are removed from it. Currently you have no control over this shrinking -- if you set a capacity and then remove an item, it may reduce the capacity to better match the current size. In the future an explicit call to set the capacity should turn off this aggressive shrinking behavior.