Home > Blockchain >  Filtering by category and ID in an ArrayList of objects
Filtering by category and ID in an ArrayList of objects

Time:12-06

I'm working on a Java assignment at university and I'm not sure what the most efficient way would be.

The assignment tells us to create a class for products containing name, quantity, etc, and a productRegister class that can add and search through the products based on ID, category and such.

The easiest way would be to have an ArrayList filled with product objects in the productRegister class, and then have methods that search through the Arraylist using Streams and filter. Java would then iterate through each object -> call a method for each object -> collect and return the ones that passes the filter. But that seams counter intuitive after working with relational DBs. It probably don't matter with a small dataset of products, but on a larger scale it don't seem efficient.

How would I go about making the data relational? I have been trying with hashTables and linkedLists, but I don't want to duplicate any data.

Or is there another way to optimise and make the search more efficient?

I have tried making a hashTable but it seams messy as the key is a duplicate of the data inside the object. The keys are also not unique, as multiple objects can have the same category/key.

I'm not allowed to use a DB for this assignment, so I'm looking for a way to query more efficiently or make the data relational inside of Java.

CodePudding user response:

Your university assignment has got you thinking on the right track. The ProductRegister class feels like an abstraction on top of a more complex data fetching system you'd want to build in the "real world"

For very large data sets in production settings, it's unlikely you'd store this data in memory.

If you needed to execute complex queries against, say, 1 billion records, it would be much more performant for the ProductRegister class to instead be a ProductRegisterClient. The ProductRegisterClient would be responsible for fetching from a data source (sql for example), where the complex queries would be evaluated.

The tradeoff is that you're introducing latency making those requests, but there are strategies to mitigate that, caching comes to mind.

CodePudding user response:

For small datasets, you can indeed use the Streams API. On a larger dataset, it would indeed be less efficient, but then it is common practice to use (relational) databases anyway instead of keeping all data in memory in your application.

  • Related