0

I have a shopping site, in which items can be added to a basket, each item is an instance of a class Product, and all the items are stored in a Collection<Product> items,

I am currently then iterating through that list and displaying each item in a table.

However I want to display a quantity value for each item instead.

I created a Map, and am trying to put each of my products into it.

However each Product is still listed as only existing once because each class instance is different?
How would I adjust this?

My Product class has a product ID value. Here's the code I have currently.

Map<Product, Integer> map = new HashMap<>();
for (Product p : items) {
    Integer i = map.get(p);
    if (i == null) {
        map.put(p, 1);
    }
    else {
        map.put(p, i+1);
    }
}

Having implemented hashcode and equals methods.

Trying to add the items to the map.

Collection<Product> items = basket.getItems();
    Map<Product, Integer> map = new HashMap<>();
    for (Product p : items) {
        for (Product key : map.keySet()) {
            if (p.equals(key)) {
                map.put(key, map.get(key));
            }
            else {
                map.put(p, 1);
            }
        }
    }
Chaz
  • 195
  • 4
  • 17
  • 3
    How are you adding the two equivalent `Product`in the map ? Is it different instances, if so, are you implementing `equals` and `hashCode()` to be able to compare two distinct instance ? In the code, what is this `items` iterable ? EDIT : well no, @SeanPatrickFloyd write it before I validate ^^ – AxelH Mar 16 '17 at 11:09
  • 2
    I'd suggest you actually creating a CartItem class, that encapsulates the product and the quantity. – Sean Patrick Floyd Mar 16 '17 at 11:09
  • do you want this? map.put(p, i+p.getQuantity()); – tak3shi Mar 16 '17 at 11:11
  • Possible duplicate of [Why does this HashMap.get return a null?](http://stackoverflow.com/questions/6078207/why-does-this-hashmap-get-return-a-null) – AxelH Mar 16 '17 at 11:16

2 Answers2

1

However each Product is still listed as only existing once because each class instance is different?

Yes.

HashMap identifies keys by using their implementation of hashcode() and equals().

You you either use a property, which already has a proper implementation of both (as @zsmb13 suggested) or you create implementations of hashcode() and equals() in your Product class (ATTENTION! do not inherit them! They must be implemented in a decent child which will not be extended itself...).

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
  • Am I understanding correctly a hashcode should return a value by hashing two values that are uniquely identifiable so if I hash the product id and it's title, every class instance that has the same product will therefore have the same hashcode? – Chaz Mar 16 '17 at 11:39
  • 1
    http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java – slim Mar 16 '17 at 11:53
0

You need to override the equals and the hashCode of Product Class for your hashing based operations to function properly. You need your Product class something like this

class Product{

    private int price;
    private String name;

    public Product(String itm, int pr){
        this.name = itm;
        this.price = pr;
    }

    public int hashCode(){
        int hashcode = 0;
        hashcode = price*20;
        hashcode += name.hashCode();
        return hashcode;
    }

    public boolean equals(Object obj){
        if (obj instanceof Product) {
            Product pp = (Product) obj;
            return (pp.name.equals(this.name) && pp.price == this.price);
        } else {
            return false;
        }
    }
}
amudhan3093
  • 740
  • 9
  • 17
  • Thanks! How do I then use this to iterate through my collection of Product instances and add them to the map if they're equal? I'll post what I've attempted in the op. – Chaz Mar 16 '17 at 12:16