我有一种方法可以读取文件的ArrayList中每个文件的上下文,并将其保存在Map <>的ArrayList中:
public ArrayList<Map<String, Integer>> mapEachWordofEachFile(ArrayList<File> listFiles, Map<String, Integer> map) throws FileNotFoundException {
ArrayList<Map<String, Integer>> listMaps = new ArrayList<>();
for (int i = 0; i < listFiles.size(); i++) {
map.clear();
Scanner fileScanner = new Scanner(listFiles.get(i));
while (fileScanner.hasNext()) {
String wordString = fileScanner.next();
Integer count = map.get(wordString);
if (count != null)
count++;
else
count = 1;
map.put(wordString, count);
}
fileScanner.close();
listMaps.add(map);
System.out.println(listMaps);
}
return listMaps;
}
它会在文件中进行迭代,但不能正确保存在地图中。