Skip to content
Advertisement

Guava Maps.difference with wildcard

I want to use Guava’s Maps.difference in java 11 to verify data from Json String which I mapped into a Map.

What I have after a call (with different uid each time) :

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

I want to verify that uid is correctly generated and name is “Jean” using a pattern like this :

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

Of course Maps.difference returns a difference in uid value…

Is it possible to specify a wildcard in my verifying pattern so that Maps.difference returns no differences ?

Thanks in advance.

Advertisement

Answer

Assuming you mean Guava’s Maps.difference: yes, use the 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; }
});
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement