Say we have a bunch of car objects.
Each car has some distinctive properties, for example. manufacturer, model, year, etc. (they can be used to create separate hash codes).
Each car has a list of PurchaseOffer objects (the PurchaseOffer object contains pricing / retail information).
We get Car Listings from several different sources, each car with one PurchaseOffer. The fact is that these lists may overlap - a car may appear in several lists.
We want to combine the lists into a single set of vehicles in which each vehicle contains all the PurchaseOffers found in it.
My problem is choosing which collection to use in this aggregation process:
It turns out to be natural to use java.util.HashSet to store our cars, thus, by going through different lists of cars, we can check whether the car exists in Set in depreciated O (1), however - you cannot get an element from the set (in our case - when we meet a car that already exists in Set - we would like to get this car from the set based on its identification hash code and add PurchaseOffers to it).
I can use a HashMap where each Car hashCode maps to a real Car object, but this is probably not a school book solution, because it is unsafe - I need to make sure that every hash code displays the car with what hashCode - there may be inconsistency. Of course, you can create a specific data structure that guarantees this consistency. Could it already exist?
Can someone suggest the data structure that I am, or indicate a design error? Thanks.
bloodcell
source share