查询java中的数据结构

时间:2012-11-25 09:13:24

标签: java map linkedhashmap

我想从多个文件中计算单词频率。

此外,我在这些文件中有这些词

a1.txt = {aaa, aaa, aaa} 
a2.txt = {aaa} 
a3.txt = {aaa, bbb} 

所以,结果必须是aaa = 3,bbb = 1.

然后,我已经定义了上述数据结构,

LinkedHashMap<String, Integer> wordCount = new LinkedHashMap<String, Integer>();
Map<String, LinkedHashMap<String, Integer>>
fileToWordCount = new HashMap<String,LinkedHashMap<String, Integer>>();

然后,我读取文件中的单词并将它们放在wordCount和fileToWordCount中:

/*lineWords[i] is a word from a line in the file*/
if(wordCount.containsKey(lineWords[i])){
   System.out.println("1111111::"+lineWords[i]);
   wordCount.put(lineWords[i], wordCount.
   get(lineWords[i]).intValue()+1);
   }else{
   System.out.println("222222::"+lineWords[i]);
   wordCount.put(lineWords[i], 1);
}
fileToWordCount.put(filename, wordCount); //here we map filename
and occurences        of       words

最后,我用上面的代码打印fileToWordCount,

Collection a;
Set filenameset;

        filenameset = fileToWordCount.keySet();    
        a = fileToWordCount.values();          
        for(Object filenameFromMap: filenameset){
                   System.out.println("FILENAMEFROMAP::"+filenameFromMap);                                 
                System.out.println("VALUES::"+a);                                                
        }

并打印,

FILENAMEFROMAP::a3.txt
VALUES::[{aaa=5, bbb=1}, {aaa=5, bbb=1}, {aaa=5, bbb=1}]
FILENAMEFROMAP::a1.txt
VALUES::[{aaa=5, bbb=1}, {aaa=5, bbb=1}, {aaa=5, bbb=1}]
FILENAMEFROMAP::a2.txt
VALUES::[{aaa=5, bbb=1}, {aaa=5, bbb=1}, {aaa=5, bbb=1}]

那么,我如何使用map fileToWordCount来查找文件中的单词频率?

2 个答案:

答案 0 :(得分:1)

你让它变得更加困难。我就是这样做的:

Map<String, Counter> wordCounts = new HashMap<String, Counter>();
for (File file : files) {
    Set<String> wordsInFile = new HashSet<String>(); // to avoid counting the same word in the same file twice
    for (String word : readWordsFromFile(file)) {
        if (!wordsInFile.contains(word)) {
            wordsInFile.add(word);
            Counter counter = wordCounts.get(word);
            if (counter == null) {
                counter = new Counter();
                wordCounts.put(word, counter);
            }
            counter.increment();
        }
    }
}

答案 1 :(得分:0)

如果我可以提出另一种方法:)

使用Map<String, Set<String>> map

foreach file f in files
  foreach word w in f
    if w in map.keys()
      map[w].add(f)
    else
      initialize map w to be a set with the only element file