Guava Maps.difference 带通配符

Guava Maps.difference with wildcard

我想在 java 11 中使用 Guava 的 Maps.difference 来验证来自 Json 我映射到 Map 中的字符串的数据。

通话后的内容(每次uid不同):

{"uid":"31a340bc-e5ed-440c-8726-34c54dea902a","name":"Jean"}

我想使用如下模式验证 uid 是否已正确生成且名称为“Jean”:

{"uid":"*","name":"Jean"}

当然Maps.differencereturnsuid值的不同...

是否可以在我的验证模式中指定一个通配符,以便 Maps.difference returns 没有区别?

提前致谢。

假设您指的是 Guava 的 Maps.difference:是的,请使用 difference(left, right, valueEquivalence) overload:

Maps.difference(left, right, new Equivalence<String>() {
  @Override public boolean doEquivalent(String a, String b) {
    return "*".equals(a) || "*".equals(b) || Objects.equals(a, b);
  }

  // You have to hash all strings to the same thing because you
  // don't know if the current item is a "*", or its corresponding
  // item is a "*". But actually it doesn't matter, because I don't
  // think it's invoked in difference.
  @Override public int doHash(String s) { return 0; }
});