I have to maintain an old application and have to extend some modules.

There is an hashtable that is used for maintaining/holding some objects as representation of running (real) processes (don't see them as plain software processes).

It was easy:

  • if a new process was created it was added as name + the object
  • to know if a process is running is simply was a check if the process name is in the table
  • if the process gets terminated remove it by the name form the table
  • sometimes there is cleanup that checks all processes if they are really running (using information form the value object in the table)

that worked fine in the past, as each process with a name could (and should) only be running only once (if already running by checking the hashtable no second one could be started). But now it should be extended and it is possible for one of these processes (only of the names) is possible to run up to 5 instances. So the name becomes some kind a type.

Now I'm thinking about how to extend the software to support this. Is there a way to extend the hashtable to support multiple values for a key (the name/type)? Or to habe something like a trible-hash-table?

Or what would be an alternative to the hashtable?

the code is implemented in Java 8.

  • 3
    There are many implementations like that e.g MultiValuedMap from apache commons or Multimap from Guava.Luckily you don't need to reinvent the wheel, although you can implement this solution on your own. Nov 15, 2018 at 13:46
  • 2
    If you don't want to include a third party library, you can of course still use a Map<K,Collection<V>> to map keys to collections of values. When adding an entry, you check if the key is already associated with a collection; if it is, add the new value to the collection. If it's not create a new collection, add the value and put the collection into the map by the key. Using Java8 this all can be done simply via map.computeIfAbsent(key, (k,v) -> new ArrayList<V>()).add(value);.
    – JimmyB
    Nov 15, 2018 at 14:06


Your Answer

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

Browse other questions tagged or ask your own question.