I have two copies of data, here 1 represents my volumes and 2 represent my issues. I have to compare COPY2 with COPY1 and find all the elements which are missing in COPY2 (COPY1 will always be a superset and COPY2 can be equal or will always be a subset). Now, I have to get the missing volume and the issue in COPY2. Such that from the following figure(scenario) I get the result as : -
Missing files – 1-C, 1-D, 2-C, 2-C, 3-A, 3-B, 4,E.
Question-
- What data structure should I use to store the above values (volume and issue) in java?
- How should I implement this scenario in java in the most efficient manner to find the difference between these 2 copies?
I suggest a flat
HashSet<VolumeIssue>
. EachVolumeIssue
instance corresponds to one categorized issue, such as1-C
.In that case all you will need to find the difference is a call
What is left in
copy1
are all the issues present incopy1
and missing fromcopy2
.Note that your
VolumeIssue
class must properly implementequals
andhashCode
for this to work.Since you've added the Guava tag, I'd go for a variation of Marco Topolnik's answer. Instead of removing one set from the other, use
Sets.difference(left, right)
You can have a HashMap's with key and value pairs.
key is Volume and Value is a List of Issues.
By getting value from both the HashMap's so you get two List's of value. Then find the difference between those two lists.
consider you got two list of values with same key from two maps.
now