java - Best way to total distinct values of a Bean inside of an ArrayList? -


i have arraylist of bean. inside bean 2 properties "string monthyear" , "double bill" need add , total bill based on monthyear.

for example based code below need output of

 jan-2013 = 210  feb-2013 = 20   nov-2012 = 130   dec-2012 = 40  public static void main(string[] args) {         list<billsbean> billlist = new arraylist<billsbean>();          billsbean bills1 = new billsbean();         bills1.setmonthyear("jan-2013");         bills1.setbill(10);         billlist.add(bills1);          billsbean bills2 = new billsbean();              bills2.setmonthyear("feb-2013");         bills2.setbill(20);         billlist.add(bills2);          billsbean bills3 = new billsbean();         bills3.setmonthyear("nov-2012");         bills3.setbill(30);         billlist.add(bills3);          billsbean bills4 = new billsbean();         bills4.setmonthyear("dec-2012");         bills4.setbill(40);         billlist.add(bills4);          billsbean bills5 = new billsbean();         bills5.setmonthyear("nov-2012");         bills5.setbill(100);         billlist.add(bills5);          billsbean bills6 = new billsbean();         bills6.setmonthyear("jan-2013");         bills6.setbill(200);         billlist.add(bills6); } 

add following method in class , call main.

private static void calculatesum(list<billsbean> billlist) {     map<string,double> summap=new hashmap<>();     for(billsbean bean:billlist)         if(summap.containskey(bean.getmonthyear()))             summap.put(bean.getmonthyear(), bean.getbill()+summap.get(bean.getmonthyear()));         else             summap.put(bean.getmonthyear(),bean.getbill());     iterator iter = summap.keyset().iterator();     while(iter.hasnext()){         string key = iter.next().tostring();         system.out.println("year: "+key+" bill: "+summap.get(key));     }  } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -