I am trying to read from files and put them in Hash Maps. When i am reading a huge file or a file with only a sentence it works fine, But when I am reading both files it throws
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
Before putting into hashmaps i put the text into a StringBuffer. Is it the StringBuffer making the heap full? My code is :
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class testforso {
public static void main(String args[]) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
ArrayList<String> fileNames = new ArrayList<String>();
ArrayList<ArrayList<ArrayList<String>>> listWithFiles = new
ArrayList<ArrayList<ArrayList<String>>>();
String fileName;
System.out.println("Enter the file names\t(Type stop to exit):");
fileName = sc.next();
while (!fileName.equals("stop")) {
fileNames.add(fileName);
fileName = sc.next();
}
for (int i = 0; i < fileNames.size(); i++) {
Litourgia1 file = new Litourgia1(fileNames.get(i));
listWithFiles.add(file.getWords());
}
System.out.println("Files read successfully");
HashMap<String, HashMap<String, Integer>> hm = new HashMap<String, HashMap<String, Integer>>();
HashMap<String, Integer> hm2 = new HashMap<String, Integer>();
for (int i = 0; i < listWithFiles.size(); i++) { // for loop to get into files
for (int j = 0; j < listWithFiles.get(i).size(); j++) { // for loop to get into sentences
for (int k = 0; k < listWithFiles.get(i).get(j).size(); k++) { // for loop to get into words
if (hm.containsKey(listWithFiles.get(i).get(j).get(k)))
hm2.put(listWithFiles.get(i).get(j).get(k), hm2.get(listWithFiles.get(i).get(j).get(k)) + 1);
else
hm2.put(listWithFiles.get(i).get(j).get(k), 1);
}
for (int k = 0; k < listWithFiles.get(i).get(j).size(); k++) { // for loop to get into words
if (!hm.containsKey(listWithFiles.get(i).get(j).get(k))) {
hm.put(listWithFiles.get(i).get(j).get(k), hm2);
}
}
}
}
System.out.println(hm.toString()); //the Exception is in this line
}
}
Thanks