Hashmap single key holding a class. count the key and retrieve counter

核能气质少年 提交于 2019-12-06 03:04:54

You can use a Map of List.

Map<String,List<FileInformation>> statistics = new HashMap<>()

In the above map, the key will be the word and the value will be a List<FileInformation> object describing the statistics of individual files containing the word. The FileInformation class can be declared as follows :

class FileInformation {
    int occurrenceCount;
    String fileName;

    //getters and setters
}

To populate the above Map, use the following steps :

  1. Read each file in the FrequencyFolder
  2. When you come across a word for the first time, put it as a key in the Map.
  3. Create a FileInformation object and set the occurrenceCount to the number of occurrences found and set the fileName to the name of the file it was found in. Add this object in the List<FileInformation> corresponding to the key created in step 2.
  4. The next time you come across the same word in another file, create a new FileInfomation object and add it to the List<FileInformation> corresponding to the entry in the map for the word.

Once you have the Map populated, printing the statistics should be a piece of cake.

for(String word : statistics.keySet()) {
  List<FileInformation> fileInfos = statistics.get(word);
  for(FileInformation fileInfo : fileInfos) {
      //sum up the occureneceCount for the word to get the total frequency
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!