-1

for example I am keeping Students school marks. So on the MultiMap I have the Student Name and Marks of all the subjects

e.g.  {Anne, { 54, 23, 54, 65, 76}

I want to generate a Map which consist of StudentName, Average marks as in this case is {Anne, 54.4}

Njax3SmmM2x2a0Zf7Hpd
  • 1,354
  • 4
  • 22
  • 44
  • 5
    Again, [what have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? Seems like you want us to write you a complete program, line by line, because [it's another basic question about Guava's `Multimap` within few minutes authored by you](http://stackoverflow.com/search?q=user%3A489818+%5Bguava%5D) and answers can be found on Wiki, in docs and in the web. – Grzegorz Rożniecki Jul 01 '13 at 10:43
  • Do you want to populate a new map ? – Rais Alam Jul 01 '13 at 10:44
  • I see a [common thread](http://stackoverflow.com/questions/17400732/whats-the-best-way-to-sum-two-mapstring-string) of questions developing here.... When you get your solution, make sure you answer your original question. ;-) – Jonathan Jul 01 '13 at 10:48
  • @Jonathan. Hi, Are you from SITA ? – Rais Alam Jul 01 '13 at 10:54
  • @RaisAlam I'm afraid I don't know what that is, sorry. – Jonathan Jul 01 '13 at 10:55
  • @Xaerxess thanks. really appreciated your assistance. In here I tried transformEntries and still out of luck figuring out. This is not explained on their documentation as far as I scanned. – Njax3SmmM2x2a0Zf7Hpd Jul 01 '13 at 10:59

1 Answers1

1
Map<String,Double> map = new HashMap<String,Double>();
for (String name : multimap.keySet())
{
     List<Integer> marks = multimap.get(name);
     int sum = 0 ;
     for (Integer num : marks )
     {
         sum += num;
     }
     double avg = (double)sum / marks.size();
     map.put(name,avg);
}
Mac
  • 1,711
  • 3
  • 12
  • 26