在java中合并两个哈希表并删除重复项
我有两个带有
对的哈希表。现在它们都有重复的值,我想合并两个哈希表以给出不同的值。我怎么能这么做呢!
谢谢 编辑 #1 我正在从目录中读取文件内容。并将这些内容作为令牌存储在两个不同的哈希表中。现在我需要将它们合并到一个哈希表中,这将为我提供两个表的不同值。
I am having two Hashtables with <int,string>
pair. Now they both have duplicate values in each of them, and I want to merge both the hashtables to give me distinct values. How can I do that!?
thanks
Edit #1 I am reading file contents from a directory. and storing those contents as tokens in two different hashtables. Now I need to merge them into a single hashtable which would give me distinct values of both the tables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜你的意思是重复的键,而不是重复的值。另外,由于 Hashtable 是一个映射实现,因此我将提供一个通用的映射解决方案。
现在map2是删除了所有重复项的合并版本
,但是如果您想要一个没有重复值的Map,那么实现起来相当麻烦。也许您应该查看 BidiMap apache commons / collections 中的接口。它提供的地图的键和值都是唯一的。
实际上,这是删除重复值的简单方法:
现在所有重复值都从两个映射中删除。 (这是假设您要完全删除重复项。如果您想保留一份副本,请在 while 循环中使用values1 和values2 集合,并且不要获取副本。)
I guess you mean duplicate keys, not duplicate values. Also, since Hashtable is a map implementation, I'll provide a generic map solution.
Now map2 is the merged version with all duplicates removed
But if you want a Map without duplicate values, that's pretty cumbersome to achieve. Maybe you should check out the BidiMap interface in apache commons / collections. It provides maps were both keys and values are unique.
Actually, here's a simple way to remove duplicate values:
Now all duplicate values are removed from both maps. (This is assuming you want to delete the dupes entirely. If you want to keep one copy, use the values1 and values2 collections in the while loops and don't take copies.)
最初的提问者在评论中断言,他指的是价值,而不是关键。这使得问题不完整。为了在两个不同的表中具有重复的值,密钥生成算法必须不同。如果是这样的话,问题中缺少的一点信息是他想保留哪把钥匙?第一个哈希表中的还是第二个哈希表中的?
SPFloyd-seanizer 上面提出的解决方案是正确的,但如果他确实是真正的意思值,则需要添加一点逻辑。这个额外的逻辑将告诉代码将东西放在合并版本中的哪个键下。
如果提问者混淆了“价值”和“关键”,那么他的解决方案就是正确的。
The original question-asker asserted in the comments that he means value, not key. This makes the question incomplete. In order to have duplicate values in two different tables, the key generation algorithm must be different. If that's the case, the bit of information that is missing from the question is which key does he want to retain? The one from the first hashtable or the one from the second?
The solution presented above by S.P.Floyd-seanizer is correct but needs a tiny bit of logic added if he really, really mean values. this additional logic would tell the code which key to put the thing under in the merged version.
if the question-asker is having 'value' and 'key' confusion then his solution is spot on.
您可以使用
Hashtable
的putAll
方法用于合并两个哈希表。作为参数传递的哈希表将覆盖原始哈希表中的重复项。我不明白两个哈希表如何合并并且仍然具有不同值。如果您想从一个哈希表中删除重复的项目(并将它们保留在第二个哈希表中),只需按照另一个答案中的建议进行操作,但不要将非重复项目添加到
map2
。之后,您将获得未修改的第一个表和包含map1
中不存在的条目的第二个表。另一个解决方案是使用 putAll 合并两个哈希表并清除第二个哈希表:)
You can use the
putAll
method ofHashtable
to merge two hashtables. The hashtable passed as an argument will overwrite duplicates in the original hashtable.I didn't get how two hashtables can be merged and still have distinct values. If you want to remove duplicate items from one hashtable (and leave them in the second hashtable) just do as suggested in another answer, but without adding non-duplicate items to
map2
. After that you will have the first table unmodified and the second with entries which aren't inmap1
.Another solution is to merge two hashtables using
putAll
and clear the second hashtable :)