2

I have abstract super class and some sub classes. My question is how is the best way to keep objects of those classes so I can easily find them using all the different parameters.

For example if I want to look up with resourceCode (every object is with unique resource code) I can use HashMap with key value resourceCode. But what happens if I want to look up with genre - there are many games with the same genre so I will get all those games. My first idea was with ArrayList of those objects, but isn’t it too slow if we have 1 000 000 games (about 1 000 000 operations).

My other idea is to have a HashTable with key value the product code. Complexity of the search is constant. After that I create that many HashSets as I have fields in the classes and for each field I get the productCode/product Codes of the objects, that are in the HashSet under that certain filed (for example game promoter). With those unique codes I can get everything I want from the HashTable. Is this a good idea? It seems there will be needed a lot of space for the date to be stored, but it will be fast.

So my question is what Data Structure should I use so I can implement fast finding of custom object, searching by its attributes (fields)

Please see the attachment: Classes Example

Thank you in advanced.

Stefan Stefanov

1
  • 3
    Use a database? Commented Sep 13, 2016 at 10:35

3 Answers 3

3

You can use Sorted or Ordered data structures to optimize search complexity. You can introduce your own search index for custom data.

But it is better to use database or search engine. Have a look at Elasticsearch, Apache Solr, PostgreSQL

Sign up to request clarification or add additional context in comments.

Comments

2

It sounds like most of your fields can be mapped to a string (name, genre, promoter, description, year of release, ...). You could put all these strings in a single large index that maps each keyword to all objects that contain the word in any of their fields. Then if you search for certain keywords it will return a list of all entries that contain that word. For example searching for 'mine' should return 'minecraft' (because of title), as well as all mine craft clones (having 'minecraft-like' as genre) and all games that use the word 'mine' in the 'info text' field.

You can code this yourself, but I suppose some fulltext indexer, such as Lucene may be useful. I haven't used Lucene myself, but I suppose it would also allow you to search for multiple keyword at once, even if they occur in different fields.

Comments

1

This is not a very appealing answer.

Start with a database. Maybe an embedded database (like h2database).

  • Easy set of fixed develop/test data; can be easily changed. (The database dump.) . Too many indices (hash maps) harm
  • Developing and optimizing queries is easier (declarative) than with data structures
  • Database tables are less coupled than data structures with help structures (maps)
  • The resulting system is far less complex and better scalable

After development has stabilized the set of queries, you can think of doing away of the DB part. Use at least a two tier separation of database and the classes.

Then you might find a stable and best fitting data model.

Should you still intend to do it all with pure objects, then work them out in detail as design documentation before you start programming. Example stories, and how one solves them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.