Say I have the following classes:
public class Tagged {
private List<String> tags;
}
public class ContainerOfTagged {
private List<Tagged> tagged;
}
With this structure, whenever I need to find a Tagged
with a specific tag, I need to iterate over all the tagged in ContainerOfTagged
, and iterating over all tags of each Tagged
. That could affect performance depending on the size of the lists.
A simple solution would be changing the ContainerOfTagged
class to use a Map
, mapping tags in lists of Tagged
:
public class ContainerOfTagged {
private Map<String, List<Tagged>> tagMapping;
}
Now all I need to do is provide a tag, and the Map
will return all Tagged
with said tag. However, by doing this I'm causing data duplication, since the same tags exist in both the Tagged
and ContainerOfTagged
classes.
So, is there a way to solve this problem with a performatic solution that doesn't duplicate data?