Java - Most efficient method for adding values associated with duplicate keys?
I have a list with a key(non-unique) and a set of values associated with that key. A key might appear more than once in the list. For all the multiple occurrences of a particular key, I need to be able to add the corresponding associated set of values.
The way I am receiving the input is that some of the values seem to be broken down into multiple rows.
(Before answering, please bear in mind that I am receiving the input in the form of Excel Sheets and I am reading from the Excel Sheet in Java using Apache POI. So, before suggesting me to change the way the data is stored to avoid redundancy, please note that this is something out of my control as is handled by another team.)
I tried using multi-dimensional arrays, but this was a lot of pain.
So, I am looking for suggestions on a better method for implementing my list/set using a Java Collection to be able to add t开发者_如何转开发he values associated with duplicate keys. I am not looking for any form of code, but just suggestions for the most efficient way to implement this. If you can just name a few methods, I can take it from there.
I truly appreciate your time for reading this.
It sounds like you want a Multimap
collection from Guava. Like a Map
, but where you get a collection of values associated with a key instead of just one.
Have a look at the Multimaps
class for common factory methods.
If by efficient, you mean easy to use and maintain, then MultiMap
from Apache Commons Collections is just the ticket.
Hash tables!
http://en.wikipedia.org/wiki/Hash_table
See the part about chaining
精彩评论