Java How to get the index of a Map of HashMap
<p>Forgive me if  it seems simple but I couldn't figure it out easily. I thought about using a loop but was wondering if anybody knows an easier way: I have:</p>

<pre><code>Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();

        map.put("ClubB", 1);
        map.put("ClubA", 2);
        map.put("ClubC", 2);
        map.put("ClubD", 2);
        map.put("ClubE", 3);
        map.put("ClubF", 2);
        map.put("ClubG", 2);
</code></pre>

<p>I need to get keys or values at specific index in my tests. For example I need to get the key and value at index 3 etc. The reason is that I use a comparator and sort the Map and would like to show that the value at a specific index has changed.</p>

<p>Thanks for any ideas.</p>

<p>UPDATE:</p>

<p>I used:</p>

<pre><code>HashMap leagueTable = new HashMap();
Map&lt;String, Integer&gt; map = sortByValues(leagueTable);

 public &lt;K extends Comparable&lt;K&gt;, 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 k1.compareTo(k2); // &lt;- To sort alphabetically
                } else {
                    return compare;
                }
            }
        };
        Map&lt;K, V&gt; sortedByValues = new TreeMap&lt;K, V&gt;(valueComparator);
        sortedByValues.putAll(map);
        return sortedByValues;
    }
</code></pre>

<p>I then used aloop to print out values:</p>

<pre><code>for (Map.Entry&lt;String, Integer&gt; entry : map.entrySet()) {

                    System.out.println( entry.getKey() + ", " + entry.getValue() );
} 
</code></pre>