Map in java - getting wrong map size : <p>I am trying to understand how Map interface works in Java. What I am trying to do is: run through the array of strings and for each name in the array <code>name[]</code> put a random <code>grade</code>between 0 to 5. Then map the <code>grade</code>to <code>name[i]</code>. However, the map size gets weird, although I have 10 elements in array, the <code>map.size()</code>is 5 after mapping. Why does the program count the same size several times (see output)? Here is the code and output below:</p>

<pre><code>    import java.util.*;  

    class MapInterfaceExample{  

    public static void main(String[] args){  

    int grade = 0; 
    String[] name = {"Lisa", "Dan", "John", "Adam", "George", "Amanda", "Sarah", "James", "Derek", "Sam"}; 

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

    for(int i=0; i&lt;name.length; i++){
    grade = (int)(Math.random()*5+1); 
    map.put(grade, name[i]);  
    //System.out.println(grade + "\t"+ name[i]);  
    System.out.println("Size of map "+ map.size());}
    } 
    }
</code></pre>

<p>Output:</p>

<p>Size of map 1</p>

<p>Size of map 2</p>

<p>Size of map 2</p>

<p>Size of map 2</p>

<p>Size of map 2</p>

<p>Size of map 3</p>

<p>Size of map 3</p>

<p>Size of map 4</p>

<p>Size of map 4</p>

<p>Size of map 5</p>
