断言具有嵌套Map字段的两个对象的不等式

时间:2017-03-03 07:40:10

标签: java junit equals assert

Iam尝试在以下类类型

的两个对象之间断言两个不等式
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;
  }
}
}

让我们假设我有以下两个CustomDr类型的对象

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)

总结一下,我想知道如何断言两个地图的交集为空?

1 个答案:

答案 0 :(得分:1)

您可以通过计算两个地图的入口集合来计算两个地图的交集。

要以非破坏性方式执行此操作(即不更改两个地图中的任何一个),您必须执行以下操作:

Map<String, Map<String, Set<String>>> intersection = new HashMap<>(map1);
intersection.entrySet().retainAll(map2.entrySet());

boolean empty = intersection.isEmpty();

本质上,您首先创建其中一个地图的副本,然后仅保留第二个地图中也存在的那些条目。如果结果不为空,则表示两个映射中都有共同的元素。

以下是您可以尝试的完整代码段:

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