private ArrayList<HashMap<String, String>> sortArrayMap(ArrayList arrList)
    {
        ArrayList retArray = new ArrayList();
        Hashtable tableUnOrdered = new Hashtable();

        for (int i = 0; i < arrList.size(); i++)
        {
            HashMap<String, String> map = (HashMap<String, String>) arrList.get(i);
            tableUnOrdered.put(map.get("TCNT"), i);
        }
        Vector v = new Vector(tableUnOrdered.keySet());
        Collections.sort(v);
        for (int j = 0; j < MAX_ITEMS_PER_GRAPH && j < v.size(); j++)
            retArray.add(v.get(j)); // add the list in the needed order

        return retArray;
    }


我无法找出此代码中发生内存泄漏的地方,任何人都可以让我知道这一点。我的老板说此代码存在内存泄漏,请我找出来。

最佳答案

坏了:我清理了泛型,并且没有返回HashMaps列表,而是返回了字符串列表。

private ArrayList<HashMap<String, String>> sortArrayMap2(ArrayList<HashMap<String, String>>  arrList)
{
    ArrayList<HashMap<String, String>> retArray = new ArrayList<HashMap<String, String>>();
    HashMap<String, Integer> tableUnOrdered = new HashMap<String,Integer>();

    for (int i = 0; i < arrList.size(); i++)
    {
        HashMap<String, String> map = arrList.get(i);

        tableUnOrdered.put(map.get("TCNT"), i);
    }
    Vector<String> v = new Vector<String>(tableUnOrdered.keySet());
    Collections.sort(v);
    for (int j = 0; j < MAX_ITEMS_PER_GRAPH && j < v.size(); j++)
        retArray.add(v.get(j)); // add the list in the needed order

    return retArray;
}

10-05 23:34