代码:
- 代码目的
- 读入所选文件,将文件内的单词依次录入HashMap<String,ArrayList<String>>内,并利用HashMap将文件内的单词与其所在的文件的文件名称关联。若一个单词出现在不同的文件中,则将这些文件的文件名统统存入对应的ArrayList中(若一个文件中有重复出现的单词,则跳过,记录一次即可)
import edu.duke.*;
import java.util.*;
import java.io.*;
public class WordsInFiles {
private HashMap<String,ArrayList<String>> wordFileMap;
public WordsInFiles(){
wordFileMap = new HashMap<String,ArrayList<String>>();
}
private void addWordsFromFile(File f) throws IOException{
Scanner input = new Scanner(f);
String currWord = null;
String currFileName = f.getName();
ArrayList<String> filesName = new ArrayList<String>();
if(f.length()!=0){
while (input.hasNext()){
filesName.clear();
currWord = input.next();
if(!wordFileMap.containsKey(currWord)){
filesName.add(currFileName);
wordFileMap.put(currWord,filesName);
}else{
if(!wordFileMap.get(currWord).contains(currFileName)){
for(String s : wordFileMap.get(currWord)){
filesName.add(s);
}
filesName.add(currFileName);
wordFileMap.put(currWord,filesName);
}
}
}
}
}
public void buildWordFileMap() throws IOException {
wordFileMap.clear();
DirectoryResource dr = new DirectoryResource();
for(File f: dr.selectedFiles()){
addWordsFromFile(f);
}
System.out.println("!!!!!!The map is listed below: ");
System.out.println(wordFileMap);
}
}
输出结果:
!!!!!!The map is listed below:
{love=[brief2.txt, brief3.txt, brief4.txt], cats=[brief2.txt, brief3.txt, brief4.txt], are=[brief2.txt], and=[brief2.txt, brief3.txt, brief4.txt], silly=[brief2.txt], dogs=[brief2.txt, brief3.txt], animals=[brief2.txt, brief3.txt], birds=[brief2.txt, brief3.txt, brief4.txt], cute=[brief1.txt], funny=[brief1.txt]}
输入文件内容:
跟进:
filesName这个ArrayList不应该放在While循环外面,而应该放在循环内部。如果放在循环外面的话,每次循环都需要清空ArrayList,后又录入其他数据,则HashMap内引用的对象数据也就随之变了。
放进HashMap的对象只是存入了一个地址,而不是对象本身。每次存入HashMap的对象需要单独保留,再申请新的对象保存新数据。