Calculating multiple car prices from a file My application needs to read a file, which includes a list of cars from different factories, and calculate the prices (by this I mean mean add up their prices).

I suppose my solution is efficient.  Is there any better approach to it?




Company Price  other details
Benz   10000   x ...
BMW    3000    y ...
Benz   1000    z ... 
Toyota 500     w ...



In this case the result should be


Benz 11000
BMW  3000
Toyota 500



Solution



public static final void main(String[] args) {
    List&lt;Company&gt; trn = new ArrayList();
    Company t = new Company();

    t.setName("Benz");
    t.setPrice(10000);
    trn.add(t);
    t = null;
    t = new Company();
    t.setName("BMW");
    t.setPrice(3000);
    trn.add(t);
    t = null;
    t = new Company();
    t.setName("Benz");
    t.setPrice(1000);
    trn.add(t);
    t = null;
    t = new Company();
    t.setName("Toyota");
    t.setPrice(500);
    trn.add(t);

    for (Company c : trn) {
        System.err.println(c.toString());
    }
    Map&lt;String, String&gt; map = new HashMap();
    int counter = 0;
    for (Company c : trn) {
        String name = c.getName();
        if (!map.containsKey(name)) {
            counter = 0;
            for (Company com : trn) {
                if (com.getName().equalsIgnoreCase(name)) {
                    counter += com.getPrice();
                }
            }
            map.put(name, String.valueOf(counter));
        }
    }
        System.err.println(map.get("Benz"));
        System.err.println(map.get("BMW"));
        System.err.println(map.get("Toyota"));
    }
}


Output




Company{name=Benz, price=10000}
Company{name=BMW, price=3000}
Company{name=Benz, price=1000}
Company{name=Toyota, price=500}
11000
3000
500
Your ArrayList and HashMap declarations are missing type arguments. Assuming if you're developing on Java 7 then all you're missing is just the diamond operator, e.g. ArrayList&lt;&gt;().

Company t = new Company();
t.setName("Benz");
t.setPrice(10000);
trn.add(t);
t = null;
t = new Company();
t.setName("BMW");
t.setPrice(3000);
trn.add(t);
t = null;
t = new Company();
t.setName("Benz");
t.setPrice(1000);
trn.add(t);
t = null;
t = new Company();
t.setName("Toyota");
t.setPrice(500);
trn.add(t);


This is an odd way of adding four objects into your List. Assuming if your Company object has the following simple constructor, you should do it as such:

trn.add(new Company("Benz", 10000));
trn.add(new Company("BMW", 3000));
trn.add(new Company("Benz", 1000));
trn.add(new Company("Toyota", 500));


Instead of a nested for loop and skipping entries that already exist in the result map, you can consider retrieving the existing calculated sum from the map, if any, and add the current price to that. Also, why is map created as Map&lt;String, String&gt; and not Map&lt;String, Integer&gt; or Map&lt;Company, Integer&gt;?

One more thing, you can consider putting this particular comparison as a method in your Company class: com.getName().equalsIgnoreCase(name).