我们的 csv 文件中有大量数据。它有 250 万行,每行有 10 个字段,我们正在尝试为每行准备 HashMap ,然后将该 HashMap 添加到 arraylist。
我无法执行此操作,因为大量数据会引发内存不足 Java 堆空间错误。
但是我的应用程序需要 HashMap 列表(我不想增加堆空间)。
reader = new CSVReader(new FileReader(dataFile),',');
Map<String, String> feedMap = null;
String[] firstLine;
String[] nextLine;
String mappingKey = null;
String mappingValue = null;
//Read one line at a time
firstLine = reader.readNext();
while ((nextLine = reader.readNext()) != null){
int i = 0;
feedMap = new HashMap<String, String>();
for(String token : nextLine){
mappingKey = xmlNodeMap.get(firstLine[i]);
if (mappingKey != null) {
mappingValue = token.trim().length() > 0 ? token : Constants.NO_VALUE;
feedMap.put(mappingKey, mappingValue);
}
i++;
}
listOfMaps.add(feedMap);
}
最佳答案
这听起来可能很油腔滑调,但您的问题是您的应用程序需要一个包含 250 万行的 List 作为 HashMap。
这是一个荒唐、不合理、坦白说可笑的要求;我无法想象这样的数据结构有什么用。
将应用程序更改为不需要它。
关于java - 添加 hashmap 和 arraylist 时出现内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684322/