简体   繁体   English

如何确定两个ImmutableMaps中具有完全相同的(键,值)对?

[英]How do I confirm that two ImmutableMaps have the exact same (key,value) pairs in them?

I need to do this for a unit test. 我需要进行单元测试。 The method being tested returns an ImmutbaleMap and I need to be able to compare it with one that I already have. 被测试的方法返回一个ImmutbaleMap,我需要能够将其与已有的方法进行比较。 One way is to get key sets for both(keySets()), run through them and compare the values returned from both maps for those keys. 一种方法是获取两个(keySets())的键集,遍历它们并比较从两个映射返回的这些键的值。 However that to me seems a little inefficient. 但是,对我来说,这似乎效率不高。 Is there a better/preferred way to do this ? 有没有更好/更好的方法来做到这一点?

If both the keys and the values implement equals() correctly, you can simply use Map.equals() : 如果键和值均正确实现equals() ,则可以简单地使用Map.equals()

Compares the specified object with this map for equality. 比较指定对象与此映射是否相等。 Returns true if the given object is also a map and the two maps represent the same mappings. 如果给定对象也是一个映射并且两个映射表示相同的映射,则返回true More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()) . 更正式地说,如果m1.entrySet().equals(m2.entrySet()) ,则两个映射m1m2表示相同的映射。 This ensures that the equals method works properly across different implementations of the Map interface. 这样可确保equals方法可在Map接口的不同实现中正常工作。

If they don't, I doubt you'll find a one-liner that works out of the box. 如果他们不这样做,我怀疑您会发现一个开箱即用的单线纸。 I expect you'd have to implement the comparison yourself. 我希望您必须自己进行比较。 It's not hard to do: 这并不难:

  • If the symmetric difference between the two key sets is not empty, you're done. 如果两个键集之间的对称差异不为空,则操作完成。
  • Otherwise, iterate over one map, looking up the same key in the other and comparing the values (using whatever comparison method is appropriate). 否则,遍历一个映射,在另一个映射中查找相同的键并比较值(使用适当的比较方法)。

This can be easily encapsulated into a helper function, perhaps parameterised by the value comparator. 可以很容易地将其封装到一个辅助函数中,也许可以通过值比较器对其进行参数设置。

Complement to @NPE's answer... 补充@NPE的答案...

Since your values do not implement .equals() / .hashCode() correctly, a simple equals on maps will not work; 由于您的值未正确实现.equals() / .hashCode() ,因此在地图上执行简单的equals无效; but you use Guava; 但是你用番石榴 theefore you have the option of implementing an Equivalence . 因此,您可以选择实现对

This means, if the class of your values is Foo : 这意味着,如果您的值的类别是Foo

  • you'll need to implement an Equivalence<Foo> : 您将需要实现Equivalence<Foo>
  • your map will have to be a Map<X, Equivalence.Wrapper<Foo>> . 您的地图必须是Map<X, Equivalence.Wrapper<Foo>>

With this, you'll be able to use Map 's .equals() . 这样,您就可以使用Map.equals()

You'll have to add values using Equivalence's .wrap() method. 您必须使用Equivalence的.wrap()方法添加值。 See here for an example of an Equivalence implementation. 有关Equivalence实现的示例,请参见此处

Another choice would be to use plain Map, write impmementation of Equivalence and use following difference method from Maps class 另一个选择是使用普通Map,编写等效的实现,并使用Maps类中的以下difference方法

MapDifference<K,V> difference(Map<? extends K,? extends V> left,
                                           Map<? extends K,? extends V> right,
                                           Equivalence<? super V> valueEquivalence)

I would prefer this way as it will not alter the Map types (won't do it just to calculate difference or to check equality). 我更喜欢这种方式,因为它不会更改Map类型(不会只是为了计算差异或检查相等性而这样做)。

Why should map1.entrySet().equals(map2.entrySet()) not work? 为什么map1.entrySet().equals(map2.entrySet())不起作用? The EntrySet.equals() method refers to the Map.contains() method which should work for your values whether they implement equals() or not (in this case you probably have an IdentityHashMap underlying). EntrySet.equals()方法引用Map.contains()方法,无论值是否实现equals() (在这种情况下,您可能都有一个IdentityHashMap作为基础),该方法都应该对您的值起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何模拟没有索引的Key,Value对(有角度)列表? - How do i mock a list of Key,Value pairs (in angular) which does not have indices? 如何在android中发布文件上传的键/值对 - How do I post key/value pairs with a file upload in android 如果我有两个值相等的键,我如何返回一个 Set<string> 有点(值+“-”+键“,”+键)?</string> - If I have two keys with equal value, how do I return a Set<String> kinda (value + " - " + key ", " + key)? 我有两种几乎相同的方法,如何重构它们? - I have two methods which are nearly the same, how to refactor them? 比较值<Key, Value>对它们进行配对和排序 - Compare values of <Key, Value> Pairs and sort them 如何同时收听多个按键输入 - How do I have multiple key inputs Listened to at the same time 如何使用Camel和Jackson库将嵌套的键值对于JSON编组? - How do I marshall nested key,value pairs into JSON with Camel and Jackson library? 如何使用Spring将嵌套键值对从属性文件加载到Java对象中? - How do I load nested key value pairs from a properties file into a Java object using Spring? 如何在JSP中创建Servlet来修改属性文件中的键值对? - How do i create a servlet in JSP to modify the key value pairs in a property file? 如何在 android 中发送带有文件上传的常规帖子键/值对 - How do I send regular post key/value pairs with a file upload in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM