Calculating sum of certain values in a list I have two loops which I am using to create a file, if the clientID in the client loop is the same as the id in the valuation loop then create a row. Then if the month value is the same as the previous month value we need to add up the values.

This all works fine but i was wondering if anyone can think of a better way?

        for (Clients cl : clientList) {
            //in loop so use stringbuilder
            StringBuilder sb = new StringBuilder();
            sb.append(cl.getId());
            sb.append(",");
            sb.append(cl.getFirstName());
            sb.append(" ");
            sb.append(cl.getLastName());
            sb.append(",");
            row = sb.toString();
            for (Valuations vl : valList) {
                year = vl.getYear();
                    //if we are on a new month
                    if (!month.equals(vl.getMonth()) &amp;&amp; !month.equals("")) {
                        //add row and reset values
                        if(!vl.isNanError()){
                        data.add(row + month + "," + year + ",£" + cash);
                        }
                        cash = 0;
                        month = "";
                        done = false;
                    }
                    if (cl.getId().equals(vl.getClientId())) {
                        if (!done) {
                            row = sb.toString();
                            cash = 0;
                            month = vl.getMonth();
                            done = true;
                        }
                    }
                    cash += vl.getValue();
            }
        }
        //add the last row after the loop breaks.
        data.add(row + month + "," + year + ",£" + cash);
If you add a toString() method inside the client class you could, replace this code:

//in loop so use stringbuilder
            StringBuilder sb = new StringBuilder();
            sb.append(cl.getId());
            sb.append(",");
            sb.append(cl.getFirstName());
            sb.append(" ");
            sb.append(cl.getLastName());
            sb.append(",");
            row = sb.toString();


for just:

row = sb.toString();


Furthermore, you can append the second with the third if condition inside the second loop, this: 

if (cl.getId().equals(vl.getClientId())) {
                    if (!done) {
                        row = sb.toString();
                        cash = 0;
                        month = vl.getMonth();
                        done = true;
                    }
                }


can became this:

  if (!done &amp;&amp; cl.getId().equals(vl.getClientId()))
    {
                            row = sb.toString();
                            cash = 0;
                            month = vl.getMonth();
                            done = true;
    }


Another thing, in the above code the row = sb.toString instruction should it be remove?
Since you are calculated the row right below the first loop:

row = sb.toString();
for (Valuations vl : valList) {