0

I am trying to sort a ArrayList of Hashtable (ArrayList>). some Hastable has 3345588 entries. When I tried to sort and assign the reverse order in hastable I found

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

my code is as below

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;
}
Smit
  • 4,685
  • 1
  • 24
  • 28

1 Answers1

1

You can try to increase vm memory when running your program. You can read here VM memory setting how to do it in details. Sample:

java -Xms1024m -Xmx4096m com.sample.HelloWorld
Community
  • 1
  • 1
kaos
  • 1,598
  • 11
  • 15