Calculating company-wise total expenses I have a variable List. Its declaration is like :


List&lt;HashMap&lt;String, Object&gt;&gt;



Sample data in it:


  [{total-expense=228, company=HiHard, year=2009}, {total-expense=2936, company=POTENT, year=2009}, {total-expense=1362412.65, company=HiHard, year=2010}, {total-expense=9007.96, company=POTENT, year=2010}, {total-expense=240427.43, company=HiHard, year=2011}, {total-expense=1956.11, company=POTENT, year=2011}]


I have written a program to fetch the below result:


  {"HiHard":[[228,1362412.65,240427.43]],"POTENT":[[2936,9007.96,1956.11]]}


i.e. to fetch company-wise total expense incurred for financial years 2009, 2010, 2011.

Current program logic:

1) getDistinctCompanies from the list

2) Iterate through the distinct companies[from step 1] and get the expense incurred by passing in company and list.

Complete program:

public class JSONConstruction {
    public static void main(String[] args) {
        List&lt;HashMap&lt;String, Object&gt;&gt; list = new ArrayList&lt;HashMap&lt;String, Object&gt;&gt;();

        ... ASSUME list has the SAMPLE data as described before.

        List&lt;String&gt; companyList = getDistinctCompanies(list);

        Iterator&lt;String&gt; iterCompany = companyList.iterator();
        String company;
        JSONObject obj = new JSONObject();
        while(iterCompany.hasNext()){
            company = iterCompany.next();
            JSONArray jsonList = new JSONArray();
            jsonList.add(getExpenseList(list, company));
            obj.put(company, jsonList);
        }

        System.out.print(obj);
    }

    private static List&lt;String&gt; getDistinctCompanies(List&lt;HashMap&lt;String, Object&gt;&gt; list) {
        Iterator&lt;HashMap&lt;String, Object&gt;&gt; iterList = list.iterator();
        String company;
        List&lt;String&gt; companyList = new ArrayList&lt;String&gt;();
        while (iterList.hasNext()) {
            HashMap&lt;String, Object&gt; map = iterList.next();
            company = (String) map.get("company");
            if(companyList==null || (!companyList.contains(company))) {
                companyList.add(company);
            }
        }
        return companyList;
    }

    private static List&lt;Object&gt; getExpenseList(List&lt;HashMap&lt;String, Object&gt;&gt; list, String company) {
        Iterator&lt;HashMap&lt;String, Object&gt;&gt; iterList = list.iterator();
        List&lt;Object&gt; expenseList = new ArrayList&lt;Object&gt;();
        String companyMatch;
        while (iterList.hasNext()) {
            HashMap&lt;String, Object&gt; map = iterList.next();
            companyMatch = (String) map.get("company");
            if(companyMatch.equals(company)) {
                Object obj = map.get("total-expense");
                expenseList.add(obj);
            }
        }
        return expenseList;
    }


Though the data is fetched correctly, I am eager to know a better way of coding this program.
private static List&lt;String&gt; getDistinctCompanies(List&lt;HashMap&lt;String, Object&gt;&gt; list) {
    Iterator&lt;HashMap&lt;String, Object&gt;&gt; iterList = list.iterator();
    String company;
    List&lt;String&gt; companyList = new ArrayList&lt;String&gt;();
    while (iterList.hasNext()) {
        HashMap&lt;String, Object&gt; map = iterList.next();
        company = (String) map.get("company");
        if(companyList==null || (!companyList.contains(company))) {
            companyList.add(company);
        }
    }
    return companyList;
}  




There is no need to check if companyList==null because that won't ever happen because 4 lines prior you create the list.  
The scope of map is defined to be inside the while loop, but company is defined outside whereas it is only used in the while loop as well.  
You should name your variables proper. Shortening names doesn't add any value. Instead of iterList a simple iterator or listIterator would be much better.  
You should leave your variables and operators some space to breathe. This helps to make your code more readable. companyList==null vs companyList == null.