I have a list of objects that contain row data for a table. There are multiple columns in that table but I don’t think that’s an issue. The logic I need to apply would require only two columns. These two columns are known as Chemical Name and Batch Number . Now the interesting part here is that a chemical can occur many times for a particular batch number. For example,
CHEMICAL | BATCH | OTHER COLUMNS
A—————-100———ABC
A—————-100———GHF
A—————-100———GHL
Now what I need is the batch number and its associated rows. I realized that the best way to do this would be HashMap where K is Batch number and V is a List. Now I am not sure how to assign a Key to a List containing row objects.
I wish but I cannot do this at the database level so that is out of question.
Or you could just use guava library. There is Multimap class(technically interface) to fit your need: One key mapping to multiple values.
You might first get all your
Rowobjects in a list, then use theMultimaps.indexmethod to create aMultimapinstance. Multimaps is a util class which provide a lot of methods to create aMultimapinstance.Your code maybe looks like:
See more details from guava api.