title: Update the values of nested hashmap in Java
body: <p>I need to update a nested hashedmap based on values taken from a file. I have am able to get the correct values from the file but only the last value gets updated to all the header values in the hashedmap. Where is the error in my code? The code is as below:</p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  import java.util.*;
  
  public class ParseMap {
      public static void main(String args[]) {
          BufferedReader reader;
          HashMap&lt;String, HashMap&lt;String, String&gt;&gt; hMap = new HashMap&lt;String, HashMap&lt;String, String&gt;&gt;();
          HashMap&lt;String, String&gt; innerMap = new HashMap&lt;String, String&gt;();
          String header = "";
          try {
              reader = new BufferedReader(new FileReader(
                      "file.txt"));
              String line = reader.readLine();
              while (line != null) {
                  if (line.contains("CallMade")) {
                      String[] words = line.split(",");
  
                      for(int i=0; i&lt;words.length; i++) {
                          if (words[i].startsWith("CallId")) {
                              header = words[i];
                          }
                      }
                      hMap.put(header, innerMap);
                  }
                  else if (line.contains("details") &amp;&amp; line.contains(header)) {
                      String[] words = line.split(",|:");
                      System.out.println(header);
  
                      for(int i=0; i&lt;words.length; i++) {
                          System.out.println(words[i]);
                          if (words[i].equals(" Agentdetails")) {
  
                              HashMap&lt;String, String&gt; inMap = hMap.get(header);
                              if (inMap == null) {
                                  hMap.put(header, inMap = new HashMap&lt;&gt;()); 
                              }
                              inMap.put("Agent", words[i+1]);
                              inMap.put("AgentID", words[i+2]);                                
                          }
                      }
                   }
             line = reader.readLine();
             }
             reader.close();
          }
          catch (Exception e) {
              e.printStackTrace();
          }
          System.out.println(hMap);
      }
  }
  </code></pre>
  
  <p><strong>file.txt</strong></p>
  
  <pre><code>[Call  ] 15:40:35.688 CallMade,Telenet,,CallId53,123456789,5674,02Apr
  [CallId53 ] Agentdetails,John,12,21
  [Call  ] 15:42:39.324 CallMade,Airtel,,CallId54,123456789,5674,02Apr
  [CallId54 ] Agentdetails,James,10,20
  [Call  ] 15:43:39.324 CallMade,Airtel,,CallId55,123456789,5674,02Apr
  [Call  ] 15:43:40.324 CallMade,Airtel,,CallId56,123456789,5674,02Apr
  [CallId55 ] Agentdetails,Jimmy,5,100
  [CallId56 ] Agentdetails,Robert,18,120
  </code></pre>
  
  <p>Here if I print the values of words list then I get the correct values but if I print the hashedmap after putting the values then only the last value of <strong>Agent</strong> and <strong>AgentId</strong> is put to all headers in the hashedmap. How to rectify the error? Thanks in advance!</p>