Search code examples
javaarrayscollectionssetstoring-data

Best way to create, search and modify (key ,value1, value2) in Java


My program is searching a log, and counting the number of times some events occur (along with the bytes associated with each event). I would like to store events of interest to me as a (key, value1, value2) where the string is the key, and the values are two longs. I need to be able to search if the key alrady exists in the structure, and I will need to be able to modify the two long values.

I am not going to go the database route or storing the data in another text file.

I was not successful in finding something using the Stack Overflow search, and the Oracle tutorials and collections documentation did little to help me. My options (as I understand it) are:

  1. a map consisting of (string, array)
  2. 2D array (where I molest strings to longs and back)
  3. a tuple object

Am I overlooking other possible solutions that are superior? And if not, which do you suggest I use?

Thanks in advance!


Solution

  • It's hard to say what you mean by "best" but this is what I'd do. I'd have a map with a string key and a value that is a simple tuple object. You "might" be able to get better performance manually but using a collections implementation of a map is the easiest to use and, more importantly, the easiest for others to understand who might need to read/maintain it later. Also, beware of premature optimization. :-)