TreeMap Iterator behaviour not consistent upon removal
<p>Today when tying to discover a bug I found the TreeMap iterator behavior a little strange when removing objects. In fact has I tested different uses, in a simple example: </p>

<pre><code>    TreeMap&lt;String, String&gt; map = new TreeMap&lt;String, String&gt;();
    map.put("1", "1");
    map.put("2", "2");
    map.put("3", "3");
    map.put("4", "4");
    map.put("5", "5");

    Iterator&lt;Map.Entry&lt;String, String&gt;&gt; iterator = map.entrySet().iterator();

     while (iterator.hasNext()) {
        Map.Entry&lt;String, String&gt; entry = iterator.next();
        System.out.println("Before "+entry.getKey());
        iterator.remove();
        System.out.println("After " +entry.getKey());
    }
</code></pre>

<p>the result is:</p>

<pre><code>Before 1
After 1 
Before 2
After 2
Before 3
After 3
Before 4
After 4
Before 5
After 5
</code></pre>

<p>But if I change it to:</p>

<pre><code>    TreeMap&lt;String, String&gt; map = new TreeMap&lt;String, String&gt;();
    map.put("1", "1");
    map.put("2", "2");
    map.put("3", "3");
    map.put("4", "4");
    map.put("5", "5");

    Iterator&lt;Map.Entry&lt;String, String&gt;&gt; iterator = map.entrySet().iterator();

            String key = "4";

    while (iterator.hasNext()) {
        Map.Entry&lt;String, String&gt; entry = iterator.next();
        if(entry.getKey().equals(key)){
            iterator.remove();
            System.out.println(entry.getKey());
        }
    }
</code></pre>

<p>Then for key = 4 the result is 5 and for key = 5 the result is 5 due to the links being changed upon removal. But why are the behaviors different. JIT? Even if that is the answer, shouldn't they be uniform?  </p>
