Two ways of doing this:
1) NULL checks every time you add an element. If the "key" is not associated with any underlying set, create a new HashSet, and place it against the key.
Set<State> states = mapping.get(transitionStringToAdd);
if (states == null) {
states = new HashSet<State>();
mapping.put(transitionStringToAdd, states);
}
states.add(transitionStateToAdd);
2) If you can afford adding a library, Google Guava provides a class just for this use case: HashMultiMap. It relieves you of the NULL checks but of course something similar is going under the hood. The new code after using this class will look exactly the same as your code snippet.
~s.o.s~
Failure as a human
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 55