我正在尝试对 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;
}