title: Averaging values from a hashmap
body: <p>I'm trying to make a method that average the values from a hashmap.
  Been trying to figure this out for a while now. The thing I'm trying to do
  is creating a program that runs a Mann-Whitney U test. In the test your supposed to take 
  ratings from two kinds of brands, merge the ratings from both brands together and rank the ratings.
  And if there is ratings that are equal, your supposed to get the average rank.
  Firstly I need to rank the whole array, from lowest to highest. I've used index +1 to get the ranks. In the code below there is three 7s in the array. The triple 7s are located a rank 6, 7 and 8. Now I need to get the average rank from those numbers (6 + 7 + 8 / 3).
  Down below is the code i've been working on.</p>
  
  <p>Here's the code: </p>
  
  <pre><code>int[] ranks = { 2, 3, 3, 5, 6, 7, 7, 7, 8, 9, 10, 10 };
  
  Map&lt;Integer, List&lt;Double&gt;&gt; m = new HashMap&lt;Integer, List&lt;Double&gt;&gt;();
  
  
  
  public void fillMap(){
  
      for (int i = 0; i &lt; ranks.length; i++) {
  
          //adds ranks to the list.
          List&lt;Double&gt; entries = m.get(ranks[i]);
  
          if (entries == null) {
              entries = new ArrayList&lt;Double&gt;();
          }
  
          //Adds index +1 to the list
          entries.add((double) (i + 1)); // 1-indexed position
  
          //adds everything to the map
          m.put(ranks[i], entries);
  
      }
      System.out.println("This is the original map: ");
      System.out.println(m);
  
  }
  
  //Method that average the map       
  public void getAverages(){
  
      Map&lt;Integer, Double&gt; newMap = new HashMap&lt;Integer, Double&gt;();
  
      double average;
      double sum = 0;
      int counter = 0;
  
      for (Entry&lt;Integer, List&lt;Double&gt;&gt; entry: m.entrySet()) {
  
          //Fills the list 'value', with the values from the map entry.
          List&lt;Double&gt; value = entry.getValue();
          int key = entry.getKey();           
  
          for(double ent : value){
  
              sum += ent;
              counter++;              
          }   
  
          average = sum / counter; 
          newMap.put(key, average);
  
  
      }
      System.out.println("\nThis is the averaged map: ");
      System.out.println(newMap);
  
  }
  
  public static void main(String [] args){
  
      NewTest nt = new NewTest();
  
      nt.fillMap();
      nt.getAverages();
  
  }
  </code></pre>
  
  <p>Not really sure why it won't work, please help.</p>