title: Remove elements List<String> inside of a HashMap in constant time
body: <p><strong>Manged to solve my problem. Instead of removing from the List I created classes containing the lists, the classes also contained an index which I incremented each time instead of removing the first element.</strong></p>
  
  <p>I'm trying to implement the Stable marriage algorithm.</p>
  
  <p>I have a <code>men = HashMap&lt;String, List&lt;String&gt;</code> where I loop over <code>men.keySet()</code></p>
  
  <p>When a certain condition is met I get a key, and I'm supposed to remove the first element of the list with that key:</p>
  
  <pre><code>int someCondition = listIWantToModify;
  List&lt;String&gt; temp = men.get(listIWantToModify);
  temp.remove(0);
  men.replace(listIWantToModify, temp)
  </code></pre>
  
  <p>I want to remove the first element from the one of the lists inside the HashMap. What happens is that I get <code>java.util.ConcurrentModificationException</code> which I guess comes from the fact that I both remove and get items from the List in the same loop. When I call the following code: </p>
  
  <pre><code>List&lt;String&gt; replaceWithP = men.get(currentPartner);
  replaceWithP.remove(0);
  men.replace(currentPartner, replaceWithP);
  </code></pre>
  
  <p>I tried to do the following:</p>
  
  <pre><code>List&lt;String&gt; replaceWithP = new ArrayList&lt;&gt;(men.get(currentPartner));
  replaceWithP.remove(0);
  men.replace(currentPartner, replaceWithP);
  </code></pre>
  
  <p>But the algorithm is supposed to be O(n<sup>2</sup>) in the worst case and I was told that when I create the new ArrayList it is O(n) thus making my algorithm O(n<sup>3</sup>) in the worst case. </p>
  
  <p>Is there anyway I can modify the list inside in constant time without getting the exception or do I need to rethink the whole structure of my implementation?</p>
  
  <p>If so I'd love some suggestions on how to do that.</p>