You really cannot avoid the βduplicationβ of tags, but remember that you do not actually duplicate them, since lists and maps contain only links to the tag string, not values ββ(however, the links probably have a lot of space in themselves).
The problem is that you need two indexes:
- You need to find the list of tags given the Tagged object.
- You need to find the Tagged object specified by the tag.
Ideally, your solution would look like this. You can solve your problems related to what you get due to synchronization by having one tag management method.
Note that in Tagged you should use Set instead of a list to avoid duplicate tags.
public class Tagged { Set<String> tags; } public class TagContainer { Map<String, Tagged> tagIndex; public tag(String tag, Tagged tagged) { tagged.tags.add(tag); tagIndex.put(tag, tagged); }
If memory usage is a serious problem, you can try some kind of link compression. Using this technique, you can store your tags in an array, and then reference them by index. If you had few of them, you could use bytes or short instead of a link, but the code would be much messy, and I would not recommend it.
EDIT:
In my first post, I suggested that Tagged be a Tagable interface. This is cleaner, but lengthens the solution, so I went back to class. However, you might consider having a Tagable interface and implementing it in the Tagged class.
public interface Tagable { Set<String> getTags; tag(String tag); }
source share