Why is the remove function not working for hashmaps?
<p>I am working on a simple project that obtains data from an input file, gets what it needs and prints it to a file. I am basically getting word frequency so each key is a string and the value is its frequency in the document. The problem however, is that I need to print out these values to a file in descending order of frequency. After making my hashmap, this is the part of my program that sorts it and writes it to a file.</p>

<pre><code>//Hashmap I create
Map&lt;String, Integer&gt; map = new ConcurrentHashMap&lt;String, Integer&gt;();
int valueMax = -1;
//function to sort hashmap
while (map.isEmpty() == false){
            for (Entry&lt;String, Integer&gt; entry: map.entrySet()){
                if (entry.getValue() &gt; valueMax){
                    max = entry.getKey();
                    System.out.println("max: " + max);
                    valueMax = entry.getValue();
                    System.out.println("value: " + valueMax);
                }
            }
            map.remove(max);
            out.write(max + "\t" + valueMax + "\n");
            System.out.println(max + "\t" + valueMax);  
        }   
</code></pre>

<p>When I run this i get:</p>

<pre><code>t 9
t 9
t 9
t 9
t 9
....
</code></pre>

<p>so it appears the remove function is not working as it keeps getting the same value. I'm thinking i have an issue with a scope rule or I just don't understand hashmaps very well.</p>

<p>If anyone knows of a better way to sort a hashmap and print it, I would welcome a suggestion.</p>

<p>thanks</p>
