Java TreeMap sorting based on values removing duplicate values
<pre><code>public class Solution {

  public static void main(String[] args) {

    HashMap&lt;String,Integer&gt; hm = new HashMap&lt;&gt;();

    hm.put("red",23);
    hm.put("orange",1);
    hm.put("yellow",32);
    hm.put("green",23);

    TreeMap&lt;String,Integer&gt; tm = new TreeMap&lt;&gt;(new ValueComparator(hm));
    tm.putAll(hm);

    for(String key : tm.keySet())
        System.out.println(key+"  "+tm.get(key));

  }
}

class ValueComparator implements Comparator&lt;String&gt;{

  Map&lt;String, Integer&gt; map;

  public ValueComparator(Map&lt;String, Integer&gt; map){
    this.map =map;
  }

  public int compare(String a, String b){
    return map.get(b).compareTo(map.get(a));
  }

}
</code></pre>

<p>Problem: Its correctly sorting based on values but is removing duplicate values. Can please someone point out the reason for that.</p>

<p>Current Output:</p>

<pre><code>yellow 32 
red  23 
orange  1 
</code></pre>

<p>Expected Output:</p>

<pre><code>yellow 32 
red  23 
green  23
orange  1 
</code></pre>

<p>I have used <code>Comparator</code> inside <code>TreeMap</code>, which in turns sorting <code>HashMap</code> based on values</p>