I need to implement a search method on a large number of objects. The strategy is the following:
There are two classes, say,
A{
String a1,
String a2,
...
}
and
B{
String b1,
String b2,
...
}
and two ArrayLists with objects of each type.
I need to find an A object where A.a1 == B.b1. If none is found, I need to find an A object where A.a2 == B.b2 and so on.. (Here == stands for value-based equality as there may be attributes of other types)
What is the best way of making this as fast as possible? The only thing I could think of so far (besides iterating over arrays) is to create a number of HashMaps with attribute values as keys and Object references as values.
Is there a better way of solving this?
You should choose the HashMap way. The lookup is very efficient because of the hashs. And the memory overhead shouldn’t be dramatic as only references are stored in the different Maps.