I have the below given data model

itemid - name - collectionitemid


itemid |name | collectionItemId
101    | abc | null
102    | def | 101
103    | ghi | 101
104    | jkl | null
105    | mno | null

In the above model, there are items and collections, there can be no collectionId for a collection and 1 item can belong to multiple collections.

I have the following use-cases

  1. get the item by name (abc -> 101)
  2. get the item by id ( 101 -> abc)
  3. get the collectionitem and its children by (name and collectionid) [Ex: if input = abc,101), result will be like

101 | abc | null 102 | def | 101 103 | ghi | 101

  1. get the item name and collectionid for a given id (101 = [abc,null]; 103 = [ghi,101])

what did i attempt so far

I tried to use a BidiMap for the id with names in java and it worked fine, however with duplicate names, it is not able to be used.

I am currently exploring in Guava table.

Help required

Please suggest me a suitable data structure that can be used here so that I can cache the data from the database and try to use the data structure for faster look-up.


1 Answer 1


If your only unique column is itemid, then that means that you are likely not going to be able to use anything past a basic Java Map<K, V> as an easy data structure.

Instead, this seems like the perfect use case for Stream<T>. Why not store each row as a data type, stuff them all into a basic List<T> (or Set<T> or Map<K, V> if you are sure that each row is unique), and then stream() over the results? Then, you can use filter to get only the info you want while still being relatively performant. For your use case, this sounds like the best result.

That said, if you really want to get as much speed as possible, you could introduce some memory redundancy in exchange for speed. Imagine you created some class called Container<A, B, C>, whose job was to hold this data as multiple different Map<K, Collection<T>> types simultaneously. And as you perform an operation on Container<A, B, C>, the operation propagates to all the collections encapsulated within. This would get complicated very quickly, and would likely make storing or manipulating data in your table very cumbersome. But if you are only concerned about fetching info as fast as possible, you probably won't get much faster than this, save for creating a cache map where all possible requests are your keys, and the results are values. But again, trying to get that to stay up to date would be difficult at best.

But at this point, you might as well just use a database. That final paragraph above is basically just recreating the concepts of indexing and caching, stuff that most database tools will do for you out of the box with very little effort from you.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.