[英]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 mapsm1
andm2
represent the same mappings ifm1.entrySet().equals(m2.entrySet())
.更正式地说,如果
m1.entrySet().equals(m2.entrySet())
,则两个映射m1
和m2
表示相同的映射。 This ensures that theequals
method works properly across different implementations of theMap
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:
这并不难:
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
:
Equivalence<Foo>
: Equivalence<Foo>
: 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.