title: Iterating twice over HashMap Java
body: <p>I have a HashMap declared as </p>
  
  <pre><code>static HashMap&lt;String,ArrayList&lt;Integer&gt;&gt; inverted_index = new HashMap&lt;String,ArrayList&lt;Integer&gt;&gt;();
  </code></pre>
  
  <p>I iterated over its keys as</p>
  
  <pre><code>    public static void printInvertedIndex() throws FileNotFoundException{
      PrintWriter writer = new PrintWriter(new File("InvertedIndex.txt"));
      Iterator it = inverted_index.entrySet().iterator();
  
      while (it.hasNext()) {
          Map.Entry pair = (Map.Entry)it.next();
          writer.println(pair.getKey() + "  " + pair.getValue());
          it.remove();
          }
  
      writer.close();
  }
  </code></pre>
  
  <p>I did all this in a function called printInvertedIndex().
  Now in some other function I want to iterate over the HashMap again, so I did this</p>
  
  <pre><code>    public static void createPermutermIndex(){
      permuterm_index.clear();
  
      Iterator it = inverted_index.entrySet().iterator();
  
      while (it.hasNext()) {
          System.out.println("here");
          Map.Entry pair = (Map.Entry)it.next();
          String temp;
          temp = pair.getKey() + "$";
          ArrayList&lt;String&gt; perms = rotations(temp);
          System.out.println(perms.size());
          for(int i=0; i&lt;perms.size(); i++)
              System.out.println(perms.get(i));
              //permuterm_index.put(temp, perms.get(i));
          it.remove();
          }
      }
  </code></pre>
  
  <p>But I am not getting "here" printed while calling createPermutermIndex(). That is, my iterator is not iterating over the entries of inverted_index.</p>
  
  <p>Is there any way I can iterate over it again?</p>