I store in a HashMap 3 types of object.
HashMap>
[\'Lorry\', [list of lorries]]
[\'Sport\', [list of sport\'s cars]]
<
A HashMap
is the right data structure for the job, but in your case you might consider using two HashMaps: One holding the relation 'Car Type' -> 'Cars of that Type', and a second one for the relation 'ID' -> 'Car with that ID'.
The basic approach is sound, however since you only want to store each instance once, a Set
is a better choice than a List
for the map entry value:
Map<String, Set<Car>> typeCache = new HashMap<String, HashSet<Car>>();
The contains()
method of HashSet
is very fast indeed, so finding if your map contains a particular instance in it values is not going to cost much.
Using two maps would probably be better though - once for each type of lookup, so also use:
Map<String, Object> idCache = new HashMap<String, Object>();