title: Using ConcurrentHashMap efficiently?
body: <p>I have a Android Application whose core component is a <code>HashMap&lt;String,float[]&gt;.</code>
  The System is having high concurrency. e.g
  here are the following three situations I have which occur frequently and they are highly overlapping in nature</p>
  
  <ol>
  <li>Iterate through all the keys in the hashmap and do some operation on
  its value(read only operations).</li>
  <li>Add new key,value pairs in the Hashmap.</li>
  <li>Remove Certain keys from the Hashmap.</li>
  </ol>
  
  <p>I do all these operations in different threads and thus am using a ConcurrentHashMap <strong>since some inconsistency in retrievals doesnt matter</strong>. e.g <strong>While iterating the map,if new entries are added then it doesnt matter to not read in those new values immediately as I ensure that next time they are read .</strong></p>
  
  <p>Also while removing the entries I am recreating the iterator everytime to avoid "ConcurrentModificationException"</p>
  
  <p>Suppose , there is a following hashmap(i.e ConcurrentHashmap)</p>
  
  <pre><code>ConcurrentHashMap&lt;String,float[]&gt; test=new ConcurrentHashMap&lt;String, float[]&gt;(200);
  </code></pre>
  
  <p>Now for Retrieval I do the following</p>
  
  <pre><code>Iterator&lt;String&gt; reader=test.keySet().iterator();
              while(reader.hasNext())
              {
                  String s=reader.next();
                  float[] temp=test.get(s);
                  //do some operation with float[] temp here(read only operation)
              }
  </code></pre>
  
  <p>and for removal I do the following</p>
  
  <pre><code>boolean temp = true;
          while (temp) {
              for (String key : test.keySet()) {
                  temp = false;
                  if (key.contains("abc")) {
                      test.remove(key);
                      temp = true;
                      break;
                  }
              }
          }
  </code></pre>
  
  <p>and when inserting in new values I simply do</p>
  
  <pre><code>test.put("temp value", new float[10]);
  </code></pre>
  
  <p>I am not sure if its a very efficient utilisation. Also it does matter not to read in removed values(however I need efficiency ,and since the iterator is again created during the function call,its guaranteed that in the next time I don't get the removed values)so that much inconsistency can be tolerated?</p>
  
  <p>Could someone please tell me an efficient way to do it?</p>
  
  <p><strong>PS. I forgot to mention that why I am doing the removal operation in such a way.
  I have now changes the condition on which its deleted from equal to contains(there might be multiple stings having the prefix "abc" followed by different suffixes. so I need to delete all those then.</strong></p>