"主" Hashtable排序和反转中的java.lang.OutOfMemoryError

时间:2013-07-25 15:45:11

标签: java sorting hashtable

我正在尝试对Hashtable的ArrayList(ArrayList>)进行排序。一些Hastable有3345588个条目。当我试图在hastable中排序并分配相反的顺序时,我找到了

  Exception in thread "main" java.lang.OutOfMemoryError
      at java.util.Hashtable.newEntry(Hashtable.java:91)
     at java.util.Hashtable.put(Hashtable.java:766)

我的代码如下

public static Hashtable<String, Integer> sortValue(
        Hashtable<String, Integer> t) {
    // Transfer as List and sort it
    ArrayList<Map.Entry<String, Integer>> l = new ArrayList<Entry<String, Integer>>(
            t.entrySet());
    Hashtable<String, Integer> f = new Hashtable<String, Integer>();
    Collections.sort(l, new Comparator<Map.Entry<String, Integer>>() {
        public int compare(Map.Entry<String, Integer> o1,
                Map.Entry<String, Integer> o2) {
            return o1.getValue().compareTo(o2.getValue());
        }
    });
    // create new normalized Hashtable index started from 1 from the most
    // frequent key
    int a = 1;
    for (int i = l.size(); i > 0; i--) {
        f.put(l.get(i - 1).getKey(), a);// getting error here
        a++;
    }
    // System.out.println(l);

    return f;
}

1 个答案:

答案 0 :(得分:1)

您可以尝试在运行程序时增加vm内存。您可以在此处阅读VM memory setting详细信息。样品:

java -Xms1024m -Xmx4096m com.sample.HelloWorld