Iam trying two assert inequality between two objects of the following class type
public class CustomDr{
private Map<String,Map<String,Set<String>>> field1;
....
....
@Override
public boolean equals(final Object obj) {
if (this == null || obj == null)
return false;
CustomDr CustomDr = null;
if (obj instanceof CustomDr) {
CustomDr = (CustomDr) obj;
}
final Map<String, Map<String, Set<String>>> appltCustomDrMap1 =
this.field1;
final Map<String, Map<String, Set<String>>> appltCustomDrMap2 =
CustomDr.field1;
for (final String applt : appltCustomDrMap1.keySet()) {
if (!appltCustomDrMap2.containsKey(applt)) {
return false;
}
}
for (final String applt : appltCustomDrMap1.keySet()) {
final Map<String, Set<String>> productappldCustomDrMap1 =
appltCustomDrMap1.get(applt);
final Map<String, Set<String>> productappldCustomDrMap2 =
appltCustomDrMap2.get(applt);
if (productappldCustomDrMap1.size() != productappldCustomDrMap2
.size()) {
return false;
}
for (final String productappld : productappldCustomDrMap1
.keySet()) {
if (!productappldCustomDrMap2.containsKey(productappld))
return false;
}
for (final String productappld : productappldCustomDrMap1
.keySet()) {
Collections.sort(new ArrayList(productappldCustomDrMap1
.get(productappld)));
Collections.sort(new ArrayList(productappldCustomDrMap2
.get(productappld)));
if (!productappldCustomDrMap1.get(productappld).equals(
productappldCustomDrMap2.get(productappld)))
return false;
}
}
}
Lets asssume i have the following two objects of CustomDr type
Obj1
POne|PT2(MN12);PT3(MN13)||PTwo|PT3(MN12);PT4(MN14)
key value key value key value key value
key -----value---------- key ------value--------
Obj2
POne|PT2(MN12);PT3(MN13)||PTwo|PT8(MN15);PT4(MN15)
To sum up , i want to know how can i assert intersection of the two Maps as empty ?
You can calculate the intersection of two maps by calculating the intersection of their entry sets.
To do this in a non-destructive way (i.e. without altering either of the two maps), you would have to do something like this:
Map<String, Map<String, Set<String>>> intersection = new HashMap<>(map1);
intersection.entrySet().retainAll(map2.entrySet());
boolean empty = intersection.isEmpty();
In essence, you first create a copy of one of the maps, and then retain only those entries that are also present in the second map. If the result is not empty, that means that there were common elements in both maps.
Here's a complete snippet for you to experiment with:
Map<String, Map<String, Set<String>>> map1 = new HashMap<>();
Map<String, Map<String, Set<String>>> map2 = new HashMap<>();
map1.put("POne", new HashMap<>());
map1.get("POne").put("PT2", new HashSet<>());
map1.get("POne").get("PT2").add("MN12");
map1.get("POne").put("PT3", new HashSet<>());
map1.get("POne").get("PT3").add("MN13");
map1.put("PTwo", new HashMap<>());
map1.get("PTwo").put("PT3", new HashSet<>());
map1.get("PTwo").get("PT3").add("MN12");
map1.get("PTwo").put("PT4", new HashSet<>());
map1.get("PTwo").get("PT4").add("MN14");
map2.put("POne", new HashMap<>());
map2.get("POne").put("PT2", new HashSet<>());
map2.get("POne").get("PT2").add("MN12");
map2.get("POne").put("PT3", new HashSet<>());
map2.get("POne").get("PT3").add("MN13");
map2.put("PTwo", new HashMap<>());
map2.get("PTwo").put("PT8", new HashSet<>());
map2.get("PTwo").get("PT8").add("MN15");
map2.get("PTwo").put("PT4", new HashSet<>());
map2.get("PTwo").get("PT4").add("MN14");
Map<String, Map<String, Set<String>>> intersection = new HashMap<>(map1);
intersection.entrySet().retainAll(map2.entrySet());
boolean empty = intersection.isEmpty();
System.out.println(empty); // false