【问题标题】:Memory efficiency with HashMap in AndroidAndroid中HashMap的内存效率
【发布时间】:2013-03-06 17:05:34
【问题描述】:

当我第一次开始了解 HashMap 时,我为我的公司编写了内部 Android 应用程序,其中包含每个订单项属性的单独地图。订单项是动态的,并且具有多个具有相应值的属性。

示例(并非所有地图都包括在内):

// Preceding onCreate()
HashMap<Integer, Double> latMap;
HashMap<Integer, Double> lngMap;
HashMap<Integer, String> descMap;
// In onCreate()
latMap = new HashMap<Integer, Double>();
lngMap = new HashMap<Integer, Double>();
descMap = new HashMap<Integer, String>();

LogCat(结构:{ItemNumber=Value, ItemNumber=Value}):

-- latMap output --
{1=0.0, 2=0.0}
-- lngMap output --
{1=0.0, 2=0.0}
-- descMap output --
{1=NULL, 2=NULL}

现在我正在测试只有 2 个地图,主要的 HashMap 包含项目编号和一个包含各个属性和值的包含地图

例子:

// Preceding onCreate()
HashMap<Integer, HashMap<String, String>> testMapFull;
HashMap<String, String> testMapContent;
// In onCreate()
testMapFull = new HashMap<Integer, HashMap<String, String>>();
testMapContent = new HashMap<String, String>();

LogCat(结构:{ItemNumber{Property=Value, Property=Value}}):

{
    1={
        object_lat=0.0,
        object_lng=0.0,
        desc=NULL,
    },
    2={
        object_lat=0.0,
        object_lng=0.0,
        desc=NULL,
    }
}

我的问题:内存效率等方面是否存在显着差异?目前一切正常。是的,我知道这两张地图必须比几张更好,但它们包含的信息量相同,需要实例化的信息更少。

【问题讨论】:

  • 如果你只有几个这样的地图,我们说的最多只有几个 1000 字节。如果内存确实受到限制,您可以通过使用指定容量和负载因子的重载构造函数来节省一些内存。

标签: android memory-management hashmap


【解决方案1】:

为什么不能声明一个自定义类来保存有关特定项目的全部信息? 它将允许您将HashMap 的数量减少到 1。

public class Item {
    public double lat;
    public double lng;

    public String desc;
}

HashMap<Integer, Item> itemMap;

它需要更少的内存,因为它只使用一个HashMap,并允许避免创建不必要的临时对象的Boxing/Unboxing 操作。

您还可以通过使用SparseArray 而不是HashMap&lt;Integer, ?&gt; 来进一步减少Boxing/Unboxing 操作的数量

【讨论】:

  • 有趣,我没有想过这个。如果我有误解,请纠正我,在您的示例中,我必须为每个行项目创建一个 Item 类的新实例。在您看来,这仍然适用于需要更少的内存吗?我会调查SparseArray,看不出我无法切换的任何原因。
  • 是的,正如我所说,只使用了一个HashMap(或SparseArray)并且没有装箱/拆箱
【解决方案2】:

基于@vmironov 提供的答案的可能解决方案,看起来我将从HashMap 切换到SparseArray,并使用包含所有变量的Item 类。

下面是我如何设置的一个简单示例,供未来的访问者使用:

SparseArray<Item> sparseTest;

// Create an instance of this class for each item
public class Item {
    public String desc;
}
int itemNumber = 1;

// Set item variables
Item item = new Item();
item.desc = "Test Description";

// Once all variables are set, put into SparseArray
sparseTest = new SparseArray<Item>();
sparseTest.put(itemNumber, item);

// My application requires this data to be stored in a JSONArray
JSONArray ja = new JSONArray();
JSONObject innerJo = new JSONObject();
JSONObject wrapperJo = new JSONObject();
try {
    innerJo.put("desc", sparseTest.get(itemNumber).desc);
    wrapperJo.put("" + itemNumber, innerJo);
    ja.put(wrapperJo);
} catch (JSONException e1) {
    e1.printStackTrace();
}

最后是输出:

[{"1":{"desc":"Test Description"}}]

由于我的订单项值可以更改或修改,只需通过以下方式完成:

sparseTest.get(itemNumber).desc = "Description Test";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多