如何比较两个地图的值
如何比较两个地图的值?我有两个包含相同值的地图,想按它们的值进行比较。这是一个例子:
Map a = new HashMap();
a.put("foo", "bar"+"bar");
a.put("zoo", "bar"+"bar");
Map b = new HashMap();
b.put(new String("foo"), "bar"+"bar");
b.put(new String("zoo"), "bar"+"bar");
System.out.println("equals: " + a.equals(b)); // obviously false
我应该如何更改代码才能获得 true?
How to compare two maps by their values? I have two maps containing equal values and want to compare them by their values. Here is an example:
Map a = new HashMap();
a.put("foo", "bar"+"bar");
a.put("zoo", "bar"+"bar");
Map b = new HashMap();
b.put(new String("foo"), "bar"+"bar");
b.put(new String("zoo"), "bar"+"bar");
System.out.println("equals: " + a.equals(b)); // obviously false
How should I change the code to obtain a true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
比较映射值是否相等的正确方法是:
换句话说(减去错误处理):
The correct way to compare maps for value-equality is to:
In other words (minus error handling):
您尝试使用连接构造不同的字符串将会失败,因为它是在编译时执行的。这两张地图都有一对;每对都将“foo”和“barbar”作为键/值,两者都使用相同的字符串引用。
假设您确实想在不引用任何键的情况下比较值集,则只是以下情况:
将
map1.values()
与map2 进行比较可能 .values()
可以工作 - 但它们返回的顺序也可能用于相等比较,这不是您想要的。请注意,使用集合有其自身的问题 - 因为上面的代码会认为 {"a":"0", "b":"0"} 和 {"c":"0"} 的映射是相等的。 .. 毕竟,值集是相等的。
如果您可以对您想要的内容提供更严格的定义,那么就更容易确保我们为您提供正确的答案。
Your attempts to construct different strings using concatenation will fail as it's being performed at compile-time. Both of those maps have a single pair; each pair will have "foo" and "barbar" as the key/value, both using the same string reference.
Assuming you really want to compare the sets of values without any reference to keys, it's just a case of:
It's possible that comparing
map1.values()
withmap2.values()
would work - but it's also possible that the order in which they're returned would be used in the equality comparison, which isn't what you want.Note that using a set has its own problems - because the above code would deem a map of {"a":"0", "b":"0"} and {"c":"0"} to be equal... the value sets are equal, after all.
If you could provide a stricter definition of what you want, it'll be easier to make sure we give you the right answer.
要查看两个映射是否具有相同的值,您可以执行以下操作:
Collection; value()
视图List
Collections.sort
这些列表等于
类似这样有效(尽管它的类型边界可以改进):
测试工具:
由于
Collections.sort
。另请参阅:
集合value()
要测试键是否相等更容易,因为它们是
Set
:另请参阅:
设置keySet()
To see if two maps have the same values, you can do the following:
Collection<V> values()
viewsList<V>
Collections.sort
those listsequals
Something like this works (though its type bounds can be improved on):
Test harness:
This is
O(N log N)
due toCollections.sort
.See also:
Collection<V> values()
To test if the keys are equals is easier, because they're
Set<K>
:See also:
Set<K> keySet()
所有这些都返回平等。他们实际上并没有进行比较,这对于排序很有用。这将更像一个比较器:
All of these are returning equals. They arent actually doing a comparison, which is useful for sort. This will behave more like a comparator:
这个问题很老了,但仍然相关。
如果您想通过值与键的匹配来比较两个映射,您可以执行以下操作:
This question is old, but still relevant.
If you want to compare two maps by their values matching their keys, you can do as follows:
既然您询问了现成的 Api……那么 Apache 的公共资源。集合库有一个 CollectionUtils 类,为集合操作/检查提供易于使用的方法,例如交集、差集和并集。
Since you asked about ready-made Api's ... well Apache's commons. collections library has a CollectionUtils class that provides easy-to-use methods for Collection manipulation/checking, such as intersection, difference, and union.
我不认为有一个“apache-common-like”工具来比较映射,因为 2 个映射的相等性非常模糊,并且取决于开发人员的需求和映射实现......
例如,如果您在 java 中比较两个哈希映射:
- 您可能只想比较键/值是否相同
- 您可能还想比较按键的排序方式是否相同
- 您可能还想比较剩余容量是否相同
...你可以比较很多东西!
当比较两种不同的地图实现时,这样的工具会做什么:
- 一张地图允许空键
- 另一个在map2.get(null)上抛出运行时异常
你最好根据你真正需要做的事情来实现你自己的解决方案,我想你已经在上面得到了一些答案:)
I don't think there is a "apache-common-like" tool to compare maps since the equality of 2 maps is very ambiguous and depends on the developer needs and the map implementation...
For exemple if you compare two hashmaps in java:
- You may want to just compare key/values are the same
- You may also want to compare if the keys are ordered the same way
- You may also want to compare if the remaining capacity is the same
... You can compare a lot of things!
What such a tool would do when comparing 2 different map implementations such that:
- One map allow null keys
- The other throw runtime exception on map2.get(null)
You'd better to implement your own solution according to what you really need to do, and i think you already got some answers above :)
如果您假设可能存在重复值,则唯一的方法是将这些值放入列表中,对它们进行排序并比较列表,即:
如果值不能包含重复值,那么您可以在不使用集合排序的情况下执行上述操作。
If you assume that there can be duplicate values the only way to do this is to put the values in lists, sort them and compare the lists viz:
If values cannot contain duplicate values then you can either do the above without the sort using sets.
您的示例中 equals 的结果显然是错误的,因为您将映射 a 及其中的某些值与空映射 b 进行比较(可能是复制和粘贴错误)。我建议使用正确的变量名称(这样您就可以避免此类错误)并使用泛型。
字符串的串联不会产生任何效果,因为它将在编译时完成。
The result of equals in your example is obviously false because you are comparing the map a with some values in it with an empty map b (probably a copy and paste error). I recommend to use proper variable names (so you can avoid these kinds of errors) and make use of generics, too.
The concatenation of your strings doesn't have any effect because it will be done at compile time.
@paweloque为了比较java中的两个映射对象,您可以将映射的键添加到列表中,对于这两个列表,您可以使用方法retainAll()和removeAll()并将它们添加到另一个公共键列表和不同键列表中。使用公共列表和不同列表的键可以迭代映射,使用 equals 可以比较映射。
下面的代码将给出如下输出:
在 {zoo=barbar, foo=barbar} 之前
在 {zoo=barbar, foo=barbar} 之后
等于: 前- barbar 后- barbar
等于: 前- barbar 后- barbar
<代码> <代码>
>
@paweloque For Comparing two Map Objects in java, you can add the keys of a map to list and with those 2 lists you can use the methods retainAll() and removeAll() and add them to another common keys list and different keys list. Using the keys of the common list and different list you can iterate through map, using equals you can compare the maps.
The below code will give output like this:
Before {zoo=barbar, foo=barbar}
After {zoo=barbar, foo=barbar}
Equal: Before- barbar After- barbar
Equal: Before- barbar After- barbar
如果有人想在 Java 8 流中做到这一点,下面就是例子。
If anyone is looking to do it in Java 8 streams below is the example.
如果您想比较两个地图,下面的代码可能会帮助您
If you want to compare two Maps then, below code may help you