Sorry for this newbie question, but I just never worked with java... My problem is I need to implement an n:m relation in java. The use case is a catalog.
- a product can be in multiple categories
- a category can hold multiple products
the classic...
My current solution is to have a mapping class that has to hashmaps.
- The key of the first hashmap is the product id and the value is a list of category ids
- The key to the second hashmap is the category id and the value is a list of product ids
This is totally redundant an I need a setting class that always takes care that the data is stored/deleted in both hashmaps.
But this is the only way I found to make the following performant in O(1):
- what products holds a category?
- what categories is a product in?
I want to avoid full array scans or something like that in every way!
But there must be another, more ellegant solution where i don't need to index the data twice! Please enlight me... I have only plain java, no database or sqlite or something available... I also don't really want to implement a btree structure if possible.