title: Sorted map won't allow removal
body: <p>I searched earlier for a way to sort TreeMaps according to the values, was able to find a function that does that, but now it seems something's wrong with the remove() method, tried various different ways to remove using a key but its not working, and to tell the truth I don't fully understand the sorting function, can someone explain what is the problem?</p>
  
  <p>The Code:</p>
  
  <pre><code>public static void main(String[] args) {
      TreeMap&lt;String,Integer&gt; theTree = new TreeMap&lt;String,Integer&gt;();
      theTree.put("A", 5);
      theTree.put("B", 3);
      theTree.put("F", 1);
      theTree.put("D", 5);
      theTree.put("E", 6);
      theTree.put("C", 7);
      System.out.println(theTree.toString());
      Map&lt;String,Integer&gt; sortedTree = new TreeMap&lt;String,Integer&gt;();
      sortedTree = sortByValues(theTree);
      System.out.println(sortedTree.toString());
      for (Map.Entry&lt;String, Integer&gt; entry : sortedTree.entrySet())
      {
          if(entry.getKey().equals("A"))
              sortedTree.remove(entry.getKey());
      }
      System.out.println(sortedTree.toString());
      theTree = new TreeMap&lt;String,Integer&gt;(sortedTree);
      theTree.remove("B");
      System.out.println(theTree.toString());
  }
  
  public static &lt;K, V extends Comparable&lt;V&gt;&gt; Map&lt;K, V&gt; sortByValues(final Map&lt;K, V&gt; map) {
      Comparator&lt;K&gt; valueComparator =  new Comparator&lt;K&gt;() {
          public int compare(K k1, K k2) {
              int compare = map.get(k2).compareTo(map.get(k1));
              if (compare == 0) return 1;
              else return compare;
          }
      };
      Map&lt;K, V&gt; sortedByValues = new TreeMap&lt;K, V&gt;(valueComparator);
      sortedByValues.putAll(map);
      return sortedByValues;
  }
  </code></pre>
  
  <p>The Output:</p>
  
  <pre><code>{A=5, B=3, C=7, D=5, E=6, F=1}
  {C=7, E=6, A=5, D=5, B=3, F=1}
  {C=7, E=6, A=5, D=5, B=3, F=1}
  {A=5, C=7, D=5, E=6, F=1}
  </code></pre>
  
  <p>The A is still in the map</p>