Create a sorted by value Map before putting any data in it
<p>I know answer to this question has been provided in many variants but I couldn't find it for my specific query. </p>

<p>I want to have a map which is sorted by values and I need to get it created before I put data into it. I came up with below code to create it</p>

<pre><code>private Map&lt;String, Integer&gt; mapUserScore = new ConcurrentSkipListMap&lt;&gt;(new Comparator&lt;String&gt;() {
    @Override
    public int compare(String o1, String o2) {
        int i1=mapUserScore.get(o2);
        int i2=mapUserScore.get(o1);
        if(mapUserScore.get(o2)!=null &amp;&amp; mapUserScore.get(o1)!=null){
            int compare =  mapUserScore.get(o2)-(mapUserScore.get(o1));
            if(compare==0)compare=-1;
            return compare;
        }else
            return 0;
    }
});
</code></pre>

<p>So basically I want entries in map sorted by integer values in descending order so that highest scorers are on top.
However upon doing this when the first key-value pair is inserted, the program exits with below exception</p>

<pre><code>Exception in thread "Thread-0" java.lang.StackOverflowError
at java.util.concurrent.ConcurrentSkipListMap.comparable(ConcurrentSkipListMap.java:658)
at java.util.concurrent.ConcurrentSkipListMap.doGet(ConcurrentSkipListMap.java:821)
at java.util.concurrent.ConcurrentSkipListMap.get(ConcurrentSkipListMap.java:1626)
</code></pre>

<p>Upon tracing, I found that line <strong>int i1=mapUserScore.get(o2)</strong> results in this exception.
Can anyone please help me to understand what could be the reason of stackoverflow here?
I am thinking that because before any item is stored in the map, code is trying to obtain it by using get() method to sort it and hence it goes into some recursive calls and results in exception.</p>
