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.
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 viamap.computeIfAbsent(key, (k,v) -> new ArrayList<V>()).add(value);
.