Searching for a word in multiple file : Process the files with hashmap JAVA
<p>I am trying to process the list of files to store the word as : </p>

<pre><code> // &lt;word,&lt;filename, &lt;count, position[]&gt;&gt;&gt; something like this

 private static void processFile(List&lt;File&gt; 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&lt;HashMap&lt;String, HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;&gt;&gt; fList;
        String[] words = readLine.split("\\W");

        for (String newWord : words) {

          String word = newWord.toLowerCase();
          ArrayList&lt;Integer&gt; position = null;
          HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt; value = null;
          HashMap&lt;String, HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;&gt; 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&lt;Integer&gt;(); // position array
              position.add(pos);

              value = new HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;();
              value.put(1, position);

              fnameWithCount = new HashMap&lt;String, HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;&gt;();
              fnameWithCount.put(fileName, value);

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


          } else {
            position = new ArrayList&lt;Integer&gt;(); // position array
            position.add(pos);

            value = new HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;();
            value.put(1, position);

            fnameWithCount = new HashMap&lt;String, HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;&gt;();
            fnameWithCount.put(fileName, value);

            fList = new ArrayList&lt;HashMap&lt;String, HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;&gt;&gt;();          
            fList.add(fnameWithCount);

            fmap.put(word, fList);

          }

          pos++;
        }
      }
      fcount++;
    }

  }

  private static HashMap&lt;String, HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;&gt; getMap(
      ArrayList&lt;HashMap&lt;String, HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;&gt;&gt; fList,
      String fileName) {


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

      }
    }

    return null;
  }

  private static HashMap&lt;String, Integer&gt; hashMapSearch(
      String stringToLookFor) throws IOException {


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

        return null;

  }
</code></pre>

<p>My output Looks like :</p>

<pre><code>{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]}}]
</code></pre>

<p>But its supposed to be :</p>

<pre><code> [{File1={64=[array of positions]}}, {File2={29=[array of positions]}},  {File3={6=[array of positions]}}]
</code></pre>

<p>Can someone tell me what is wrong here ? Also is it good to use the map for storing  like this :     <code>// &lt;word,&lt;filename, &lt;count, position[]&gt;&gt;&gt; something like this</code>
or Can I use like this :</p>

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

<p>Can someone suggest which is good or what is wrong with my code ?</p>