In my java coding, I often end up with several Map<String,Map<String,foo>> or Map<String,List<String>> and then I have trouble remembering which String is which key. I comment the declaration with //Map<capabiltyId,Map<groupId,foo>> or //Map<groupId,List<capabilityId>, but it’s not the greatest solution. If String wasn’t final, I would make new classes CapabilityId extends String and GroupId extends String, but I can’t. Is there a better way to keep track of which thing is the key and maybe have the compiler enforce it?
In my java coding, I often end up with several Map<String,Map<String,foo>> or Map<String,List<String>> and
Share
Instead of having
CapabilityIdextendString,CapabilityIdcould include aStringfield called “id”; then yourMapcould be defined asMap<CapabilityId, Map<GroupId, Foo>>, and you could get at the individual ID fields through agetId()on your key classes.I’m not sure I would do this myself, but if I did, this is probably what I’d do.
You could limit the clutter by having an
abstract GenericIdclass with an id field andgetId()method, and haveCapabilityIdandGroupIdinherit from it.