title: Java Properties variabile: set property while iterating keys
body: <p>I have a code which collects all the system-properties keys which starts with a given prefix</p>
  
  <pre><code>private static List&lt;String&gt; getKeysByPrefix(String prefix) {
      Set&lt;?&gt; keySet = System.getProperties().keySet();
      Iterator&lt;?&gt; iterator = keySet.iterator();
  
      List&lt;String&gt; list = new ArrayList&lt;&gt;();
      while (iterator.hasNext()) {
          String key = (String) iterator.next();
  
          if (key.startsWith(prefix)) {
              list.add(key);
          }
      }
  
      return list;
  }
  </code></pre>
  
  <p>This code is running inside a Jetty web-server, and while a request is executing this code, it can happen that another request write a new key inside the java System properties like a simple <code>System.setProperty("test", "foo");</code></p>
  
  <p>With this scenario I'm getting a <code>ConcurrentModificationException</code> while I'm iterating the properties and another request changes it.</p>
  
  <pre><code>java.util.ConcurrentModificationException
      at java.util.Hashtable$Enumerator.next(Hashtable.java:1378)
  </code></pre>
  
  <p>The code which set the new property is totally another flow compared to the one is iterating the keys, but because the SystemProperties are shared among all the JVM instance, I'm getting this error.</p>
  
  <p>I already found other topics which treats this topic, and the most suggested solution is using ConcurrentHashMap. This option is unavailable for me because I'm not the one which creates the SystemProperties map but i'm only reading/writing it.</p>
  
  <p>In order to reproduce the issue you can simply add a <code>setProperty</code> call right before the closing bracket of the iterator loop</p>
  
  <p>Which is a suggested practice here in order to solve the problem?</p>