I think this would be easier if you maintain the resources in a map (instanceId -> resource) instead of set.