Reputation: 1232
What will be the best data structure to store different types of objects that are subtypes of certain class. I will need to select and print object of the same type or object that have attribute set to certain value. So the data structure must be easy and quick to search. I wonder if ArrayList will do the job.
Upvotes: 1
Views: 379
Reputation: 328568
It depends on how "quick" you need the results. Unless milliseconds count and/or you have millions of entries, an arraylist would be fine.
So I would keep it simple unless performance is not good enough (measured perfomance).
In that case, you could use a hashmap, where the key is the type of the object and the value is an arraylist of all corresponding objects (assuming that most of the time you need to query on object type). That will give you an O(1) search.
If you have different types of queries, all equally probable, you can maintain one map per type of query - it then becomes a compromise between speed and memory usage.
Upvotes: 2
Reputation:
I would personally suggest a B+ tree
with topmost node as base class.
Upvotes: 1