Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to sum hashmap values with same key java

Tags:

java

hashmap

So I am having a problem where I have to add up all values with the same key in my HashMap. The data(petshop and pet price) is retrieve from an ArrayList. at the moment, the program only gets the last value for each shop since there are multiple shops with the same name but different pet price. I would like to be able to sum the pet price for each shop. so if we have for example,
Law Pet shop: 7.00
and another Law Pet shop: 5.00,
I would like to output it like this:
Law Pet shop: 13.00.
Here is the code and output:

public class AverageCost {

    public void calc(ArrayList<Pet> pets){

        String name = "";
        double price = 0;
        HashMap hm = new HashMap();

        for (Pet i : pets) {
            name = i.getShop();
            price = i.getPrice();

            hm.put(name, price);
        }

        System.out.println("");
        // Get a set of the entries
        Set set = hm.entrySet();
        // Get an iterator
        Iterator i = set.iterator();
        // Display elements
        while(i.hasNext()) {

            Map.Entry me = (Map.Entry)i.next();
            System.out.print(me.getKey() + ": ");
            System.out.println(me.getValue());
        }
    }
}

At the moment this is the output:

Aquatic Acrobatics: 7.06
The Briar Patch Pet Store: 5.24
Preston Pets: 18.11
The Menagerie: 18.7
Galley Pets: 16.8
Anything Except Badgers: 8.53
Petsmart: 21.87
Morris Pets and Supplies: 7.12

like image 241
Daniel O Mensah Avatar asked Mar 25 '26 17:03

Daniel O Mensah


2 Answers

First, please program to the interface (not the concrete collection type). Second, please don't use raw types. Next, your Map only needs to contain the name of the pet and the sum of the prices (so String, Double). Something like,

public void calc(List<Pet> pets) {
    Map<String, Double> hm = new HashMap<>();
    for (Pet i : pets) {
        String name = i.getShop();
        // If the map already has the pet use the current value, otherwise 0.
        double price = hm.containsKey(name) ? hm.get(name) : 0;
        price += i.getPrice();
        hm.put(name, price);
    }
    System.out.println("");
    for (String key : hm.keySet()) {
        System.out.printf("%s: %.2f%n", key, hm.get(key));
    }
}
like image 140
Elliott Frisch Avatar answered Mar 28 '26 06:03

Elliott Frisch


In Java 8 you could use streams api to do this:

Map<String, Double> map = 
        pets.stream().collect(
            Collectors.groupingBy(
                Pet::getShop,
                Collectors.summingDouble(Pet::getPrice)
            )
        );
like image 45
krasinski Avatar answered Mar 28 '26 06:03

krasinski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!