2016-01-29 24 views
-1

我想处理的文件列表存储字为:搜索在多个文件中的一句话:与HashMap的Java进程文件

// <word,<filename, <count, position[]>>> something like this 

private static void processFile(List<File> list) throws IOException { 

    int fcount = 0; 
    for (File file : list) { 
     String fileName = file.getName(); 
     // System.out.println(fileName); 
     FileInputStream fstream = new FileInputStream(file); 
     BufferedReader in = new BufferedReader(new InputStreamReader(fstream)); 
     String readLine = ""; 
     int pos = 0; 
     while ((readLine = in.readLine()) != null) { 
     ArrayList<HashMap<String, HashMap<Integer, ArrayList<Integer>>>> fList; 
     String[] words = readLine.split("\\W"); 

     for (String newWord : words) { 

      String word = newWord.toLowerCase(); 
      ArrayList<Integer> position = null; 
      HashMap<Integer, ArrayList<Integer>> value = null; 
      HashMap<String, HashMap<Integer, ArrayList<Integer>>> fnameWithCount = null; 

      if (fmap.containsKey(word)) { 
      fList = fmap.get(word); 

      if(fList.contains(fileName)) { 
       fnameWithCount = getMap(fList,fileName); 
      } 

      int newCount =1; 
      if (fnameWithCount != null) { 
       value = fnameWithCount.get(fileName); 
       for (int prevcount : value.keySet()) { 
       newCount = prevcount +1; 
       value.put(newCount,value.remove(prevcount));    
       } 
       value.get(newCount).add(pos); 
      } else { 
       position = new ArrayList<Integer>(); // position array 
       position.add(pos); 

       value = new HashMap<Integer, ArrayList<Integer>>(); 
       value.put(1, position); 

       fnameWithCount = new HashMap<String, HashMap<Integer, ArrayList<Integer>>>(); 
       fnameWithCount.put(fileName, value); 

       fList.add(fnameWithCount); 
       fmap.put(word, fList); 
      } 


      } else { 
      position = new ArrayList<Integer>(); // position array 
      position.add(pos); 

      value = new HashMap<Integer, ArrayList<Integer>>(); 
      value.put(1, position); 

      fnameWithCount = new HashMap<String, HashMap<Integer, ArrayList<Integer>>>(); 
      fnameWithCount.put(fileName, value); 

      fList = new ArrayList<HashMap<String, HashMap<Integer, ArrayList<Integer>>>>();   
      fList.add(fnameWithCount); 

      fmap.put(word, fList); 

      } 

      pos++; 
     } 
     } 
     fcount++; 
    } 

    } 

    private static HashMap<String, HashMap<Integer, ArrayList<Integer>>> getMap(
     ArrayList<HashMap<String, HashMap<Integer, ArrayList<Integer>>>> fList, 
     String fileName) { 


    for (int i =0 ;i <fList.size();i++) { 
    if(fList.get(i).containsKey(fileName)) { 
     return fList.get(i); 

     } 
    } 

    return null; 
    } 

    private static HashMap<String, Integer> hashMapSearch(
     String stringToLookFor) throws IOException { 


    if (fmap.containsKey(stringToLookFor.toLowerCase())) { 
    System.out.println(fmap.get(stringToLookFor.toLowerCase()).toString()); 
    } 

     return null; 

    } 

我的输出看起来像:

{File1={1=[0]}}, {File1={1=[37]}}, {File1={1=[72]}}, {File1={1=[93]}}, {File1={1=[106]}}, {File1={1=[111]}}, {File1={1=[120]}}, {File1={1=[123]}}, {File1={1=[132]}}, {File1={1=[143]}}, {File1={1=[170]}}, {File1={1=[178]}}, {File1={1=[187]}}, {File1={1=[191]}}, {File1={1=[203]}}, {File1={1=[212]}}, {File1={1=[219]}}, {File1={1=[228]}}, {File1={1=[232]}}, {File1={1=[249]}}, {File1={1=[253]}}, {File1={1=[260]}}, {File1={1=[272]}}, {File1={1=[279]}}, {File1={1=[284]}}, {File1={1=[305]}}, {File1={1=[333]}}, {File1={1=[337]}}, {File1={1=[340]}}, {File1={1=[351]}}, {File1={1=[367]}}, {File1={1=[377]}}, {File1={1=[391]}}, {File1={1=[403]}}, {File1={1=[420]}}, {File1={1=[427]}}, {File1={1=[437]}}, {File1={1=[445]}}, {File1={1=[474]}}, {File1={1=[479]}}, {File1={1=[485]}}, {File1={1=[495]}}, {File1={1=[519]}}, {File1={1=[522]}}, {File1={1=[526]}}, {File1={1=[529]}}, {File1={1=[555]}}, {File1={1=[571]}}, {File1={1=[582]}}, {File1={1=[604]}}, {File1={1=[607]}}, {File1={1=[611]}}, {File1={1=[618]}}, {File1={1=[623]}}, {File1={1=[628]}}, {File1={1=[640]}}, {File1={1=[644]}}, {File1={1=[663]}}, {File1={1=[683]}}, {File1={1=[689]}}, {File1={1=[699]}}, {File1={1=[708]}}, {File1={1=[715]}}, {File1={1=[725]}}, {File2={1=[0]}}, {File2={1=[5]}}, {File2={1=[100]}}, {File2={1=[140]}}, {File2={1=[145]}}, {File2={1=[153]}}, {File2={1=[171]}}, {File2={1=[184]}}, {File2={1=[207]}}, {File2={1=[210]}}, {File2={1=[214]}}, {File2={1=[219]}}, {File2={1=[223]}}, {File2={1=[234]}}, {File2={1=[244]}}, {File2={1=[247]}}, {File2={1=[259]}}, {File2={1=[264]}}, {File2={1=[270]}}, {File2={1=[273]}}, {File2={1=[278]}}, {File2={1=[326]}}, {File2={1=[331]}}, {File2={1=[340]}}, {File2={1=[355]}}, {File2={1=[367]}}, {File2={1=[371]}}, {File2={1=[375]}}, {File2={1=[378]}}, {File3={1=[47]}}, {File3={1=[68]}}, {File3={1=[73]}}, {File3={1=[102]}}, {File3={1=[121]}}, {File3={1=[161]}}] 

但它应该是:

[{File1={64=[array of positions]}}, {File2={29=[array of positions]}}, {File3={6=[array of positions]}}] 

有人能告诉我这里有什么问题?也就是它很好用的地图存储这样的:// <word,<filename, <count, position[]>>> something like this 或者我可以使用这样的:

class SearchWord { 
     String word; 
     HashMap<String, Integer> fileNameWithCount; // <fileName, count> 
     HashMap<String, List<Integer>> positionList; //<fileName, pos[]> 
     } 

可有人建议这是好还是有什么错我的代码?

for (int i = 0; i < fList.size(); i++) { 
    if (fList.get(i).containsKey(fileName)) { 
    fnameWithCount = getMap(fList,fileName); 
    } 
} 

之前,你试图使用上ArrayList<HashMap<String, HashMap<Integer, ArrayList<Integer>>>>(); 的“包含”的方法来看看如果文件名是已在列表中:

+1

如果您有位置列表,则不需要计数,因为获取列表大小并不重要。但是你将不得不格式化输出。否则,地图通常是不好的数据结构,因为它们没有显示任何语义,但你似乎在做一个分配,所以它可能是好的。 –

+0

@MisrableVariable我想弄清楚哪个是最好的方式来存储索引,使搜索更容易。 – Sarah

+0

“应该是”输出是一个单词,对不对? 'class Matches {String fileName;列表职位}; class Word {字串;列表匹配; };列表;''看起来最合适,使用正确的'toString()',因此打印时不必格式化。 –

回答

0

如果你改变了“如果”条件它将工作。但是那次检查每次都是虚假的,估计为 。

+0

但只有第一个文件已更改为预期。有什么方法可以根据地图的关键字在ArrayList中获取地图吗?不用循环?它工作正常与循环通过fList大小 – Sarah

+0

@Sarah,我的不好,我编辑了我的答案。是的,你可以根据文件名作为键来获取地图。我没有看到它可能没有使用循环,因为你必须检查文件名是否出现在列表中的任何地方,所以我们需要一个循环 – sparth