0

I have a program with three objects one of them is a 6 variable object that represents the columns in a ResulSet. The other two have 3 varialbes and they match up with the first and last 3 from my other 6 variable object. I have a List<6 Var object> that represents the ResultSet records itself. I am trying to parse through a List of 6 Var Object to condense the result set so that i have only one record for each key of my smaller 3 variable object. I am using a hash map of the two 3 var objects to achieve this. the below is my method itself which takes a List and returns the same but condensed. ItemQuantities is my 6 variable object. CustomerProductClass is my 3 value key and Item is the 3 value key that i want to be unique in the returned list. You can see that I am trying to compare the current HashMap contains the key of the current object im on and steps through logic for whether or not it should remove the existing record and put itself in or not. Let me know if you can see why my first if statement always passes and elses are never reached.

public static List<ItemQuantities> getCondensedItemQuantities (List<ItemQuantities> fullItems) {
    Map<CustomerProductClass,Item> groupedProductQuantities = new  HashMap<CustomerProductClass,Item>();//making the map to put shit in

    for(int i = 0;i<fullItems.size();i++){
        ItemQuantities itemQuantities;
        itemQuantities = fullItems.get(i);
        Item item  = new Item(itemQuantities.itmType, itemQuantities.quant, itemQuantities.unitPrice);
        CustomerProductClass myCustomerProductClass = new CustomerProductClass(itemQuantities.customer,itemQuantities.location, itemQuantities.prodClass);


        if(!groupedProductQuantities.containsKey(myCustomerProductClass)){
            groupedProductQuantities.put(myCustomerProductClass,item);
        }
        else{
            if(item.Quantity > groupedProductQuantities.get(myCustomerProductClass).Quantity){
                groupedProductQuantities.remove(myCustomerProductClass);
                groupedProductQuantities.put(myCustomerProductClass, item);
            }
            if(item.Quantity == groupedProductQuantities.get(myCustomerProductClass).Quantity){
                if(item.UnitPrice > groupedProductQuantities.get(myCustomerProductClass).UnitPrice){
                    groupedProductQuantities.remove(myCustomerProductClass);
                    groupedProductQuantities.put(myCustomerProductClass, item);
                }
            }
        }
    }
    List<ItemQuantities> finalList= new ArrayList<ItemQuantities>();
    for(CustomerProductClass customerProductClass : groupedProductQuantities.keySet()){
        ItemQuantities itemQuantities = new ItemQuantities(customerProductClass.Customer,customerProductClass.Location,customerProductClass.ProductClass,groupedProductQuantities.get(customerProductClass).Name,groupedProductQuantities.get(customerProductClass).Quantity,groupedProductQuantities.get(customerProductClass).UnitPrice);
        finalList.add(itemQuantities);
    }
    return finalList; //return the hash map combined into a list of ItemQuantities.
}
  • 2
    did you override the `hashCode` and `equals` methods in `CustomerProductClass`? – Loris Securo Dec 17 '16 at 23:56
  • wow that worked thanks Loris. Can you explain why that must be done in order to have it parse correctly? I've read that it has something to do with containsKey(); compares hashcode as well as whats contianed in the keys themselves to return true/false? – Brendan Robert Dec 18 '16 at 00:07
  • @BrendanRobert It first compares hahcodes, and if those are equal, then it will check the contents (using the overiding `.equals`). But with the default `hashCode` implementation, the hashcodes are more or less always unique. – Jorn Vernee Dec 18 '16 at 00:14

0 Answers0