title: Modifying the class level hashmap changes reflecting in local reference
body: <p>I am trying to save insurance details to database based on insuranceid or updating if the insuranceid is already exists in the database. The problem is that when I am trying to save multiple insurance details it is giving ConcurrentModificationException. While debugging I found that 1st cycle it is ok that is 1st insurance details is successfully saving to database. But when next insurance details is storing it is giving the Exception. Another thing is that here I am modifying main HashMap(patientId,HashMap(insuranceId,InsuranceInfo)), and the changes are reflecting in local reference that is the id is adding to insuranceMap variable in the current method(). Please try to help me</p>
  
  <p>this is my storing method...</p>
  
  <pre><code>public void storeInsuranceInformationToDataBase(int patientId) throws SQLException
  {
      //It gives arrayList of isuranceIds which are already in the data base.
      ArrayList&lt;Integer&gt; insuranceIdsListInDatabase = getInsuranceIdsListInDatabase();  
  
      //It returns HashMap of insurance ids for given patient id. 
      HashMap&lt;Integer, InsuranceInfo&gt; insuranceMap = getInsuranceDetails(patientId);   
      Set&lt;Integer&gt; insuranceIdsInHashMap = insuranceMap.keySet();
  
      //Here I am getting ConcurrentModificationException
      for (int insuranceIdInHashMap : insuranceIdsInHashMap)
      {
          //Here I am comparing the both ids..
          if (insuranceIdsListInDatabase.contains(insuranceIdInHashMap))  
          {
              String updateInsuranceQuery = "UPDATE jp_InsuranceInfo SET  typeOfPolicy='" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "', amount=" + insuranceMap.get(insuranceIdInHashMap).getAmount() + ", duration=" + insuranceMap.get(insuranceIdInHashMap).getDuration()
                      + ", companyName='" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "' WHERE insuranceId=" + insuranceIdInHashMap + ";";
  
              getDataBase().getStatement().executeUpdate(updateInsuranceQuery);
          }
          else
          {
              String insertInsuranceQuery = "INSERT INTO jp_InsuranceInfo VALUES(" + insuranceMap.get(insuranceIdInHashMap).getPatientId() + ",'" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "'," + insuranceMap.get(insuranceIdInHashMap).getAmount() + ","
                      + insuranceMap.get(insuranceIdInHashMap).getDuration() + ",'" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "')";
  
              getDataBase().getStatement().executeUpdate(insertInsuranceQuery);
  
              ResultSet generatedInsuranceId = getDataBase().getStatement().executeQuery("SELECT SCOPE_IDENTITY()");
              int newInsuranceId = 0;
              if (generatedInsuranceId.next())
              {
                  newInsuranceId = generatedInsuranceId.getInt(1);
              }
  
              //Here it returns an insurance object which contains insurance details
              InsuranceInfo insuranceObject = insuranceMap.get(insuranceIdInHashMap); 
              insuranceObject.setInsuranceId(newInsuranceId);
              //Here I am trying to store the newly inserted insurance object into main hashmap
              storeInsuranceInfoToHashMap(patientId, insuranceObject);
  
              //I am removing previous insurance object with temporary insurance id
              getInsuranceMap().get(patientId).remove(insuranceIdInHashMap);
  
              // Adding newly generated insurance id to array list
              getInsuranceIdsListInDatabase().add(newInsuranceId);
          }
      }
  </code></pre>