使用双键创建哈希图
我正在寻找适合我的问题的数据结构。我希望能够使用两个键尽可能有效地选择节点对象。插入和删除也需要高效。基本上每个节点对象都有一对两个键。这些对是唯一的,但各个密钥却不是。我需要能够选择一组具有两个键之一的特定值的节点。
示例:
Node1 具有键 a1 和 b1
Node2 具有键 a1 和 b2
Node3 具有键 a2 和 b2
我希望能够选择具有键 a1,b1 的节点,而且还能够选择将 b2 作为 key2。
我当然可以制作两个 HashMap(每个键一个),但这是一种丑陋的解决方案,因为当我添加或删除某些内容时,我必须在两个映射中都执行此操作。由于会进行大量的添加和删除操作,因此我更愿意一次性完成此操作。有人对如何做到这一点有任何想法吗?
显然,使用一个将两个键合并在一起的键并不能解决问题,因为我还需要能够搜索单个键,而不必搜索整个地图。那效率不会很高。问题是效率问题。我可以只搜索映射中的每个条目以查找特定键,但我想使用哈希,以便我可以立即使用两个键之一选择多个节点对象。
我不是在寻找像 MultiKeyMap 这样的东西,因为在这个数据结构中,第一个键始终保持不变,您只能添加键,而不是用不同的键替换第一个键。我希望能够在第一个和第二个键之间切换。
我确实不想也不想使用相同的密钥存储多个对象。如果您查看示例,您会发现这两个键在一起始终是唯一的。这可以被视为单个键,因此我不会在同一键下存储多个对象。但是,如果您查看各个键,这些键并不是唯一的,因此我确实想存储各个键引用的多个对象。
I am looking for an appropriate data structure for my problem. I would like to be able to select node objects as efficiently as possible using two keys. Insertion and deletion also needs to be efficient. Basically every node object has a pair of two keys. The pairs are unique but the individual keys are not. I need to be able to select a group of nodes that have a particular value for one of the two keys.
Example:
Node1 has keys a1 and b1
Node2 has keys a1 and b2
Node3 has keys a2 and b2
I would like to for example be able to select the node with the key a1,b1 but also all nodes that have b2 as key2.
I could of course make two HashMaps (one for each key) but this is kind of an ugly solution because when I add or remove something I would have to do it in both maps. Since there will be a lot of adding and removing going on I would prefer to do this in one go. Does anyone have any ideas about how to do this?
Obviously having a single key that merges the two keys together does not solve the problem because I also need to be able to search for a single key without having to search through the whole map. That wouldn't be very efficient. The problem is an efficiency problem. I could just search every entry in the map for a particular key but instead I would like to use a hash so that I can select multiple node objects using either one of the two keys instantly.
I am not looking for something like the MultiKeyMap because in this data-structure the first key always stays the same, you can only add keys instead of replacing the first key with a different key. I want to be able to switch between the first and the second key.
I do and do not want to store multiple objects with the same key. If you look at the example you can see that the two keys together are always unique. This can be seen as a single key therefore I would not store multiple objects under the same key. But if you look at the individual keys, these are not unique therefore I do want to store multiple objects referenced by the individual keys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您可以使用库,请查看 Guava 的 Table 接口。它将行和列与一个值关联起来。行和列可能是您的第一个和第二个键。您还可以按行或按列进行搜索。
此接口的实现之一是 hash基于。
If you can use a library, take a look at the Table interface of Guava. It associates a row and a column with a value. The row and columns may be your first and second keys. Also you can have searches by row or by column.
One of the implementations of this interface is hash based.
您必须创建一个关键类(相等被视为 Point ):
用于选择第一个字段中具有特定值的键:
You have to create a key class (equality is treated as Point):
For selecting keys with one specific value in the first field:
由于这对于您当前的问题来说相当具体,因此您很可能需要开发自己的集合。我会包装两个
MultiMap
s 从 Apache Commons 到我自己的集合类中,该集合类同时处理两个多映射的更新,并使用我的类执行插入和查询。Since this is rather specific to your problem at hand, you will very likely need to develop your own collection. I would wrap two
MultiMap
s from Apache Commons into my own collection class that deals with updates of both multi-maps at the same time, and use my class to perform inserts and queries.编写一个简单的类,该类能够包含两个值(键)并重写 equals(..) 和 hashCode() 以进行映射使用的相等检查。使用这个简单的类作为地图的键。
在这里您可以找到一个哈希图兼容的配对类(第二个答案):
C++ Pair等价于什么?在Java中?
Write a simple class that is able to contain two values (the keys) and override equals(..) and hashCode() for equality checks used by the map. Use this simple class as the key for the map.
Here you can find a hashmap compatible pair class (2nd answer):
What is the equivalent of the C++ Pair<L,R> in Java?
由于 HashMap 只能对每个对象的一个散列进行排序,因此您将永远无法“开箱即用”选择不同的列表。我建议使用具有两个键的元组,然后迭代 HashMap 并仅选择那些具有 tuple.key1=X 的元素。
Since a HashMap can only sort on one hash for every object, you will never be able to select the distinct lists 'out of the box'. What I would suggest is using a Tuple with two keys, and then iterate over the HashMap and select only those elements that have tuple.key1=X.
HashMap 可以将任何对象作为键,所以为什么不创建一个具有 2 个字段的类并将此类视为您的键。您还可以重写 Equals 方法来告诉它键如何相等
HashMaps can have any object as Key so why not create a class with 2 fields and consider this class as your key. you can also Override the Equals method to tell it how the keys are equals
我认为我们可以这样做:对于每个键,我们可以计算相应的哈希码。
然后我们有一个二维数组,有 N 列和 N 行。
然后我们将该项目存储在
array[rowIndex][columnIndex]
中。在此实现中,您可以获得具有目标 key1 和任意 key2 的所有条目。您还可以获取具有目标 key2 和任意 key1 的所有条目。
当存在大量冲突时,该数组可能会扩展,就像对普通哈希图所做的那样。
I think we can do it in this way: For each key, we can compute the corresponding hashcode.
Then we have a 2-d array, with N columns and N rows.
Then we store the item in
array[rowIndex][columnIndex]
.In this implementation, you can get all the entries with a target key1, and any key2. You can also get all the entries with a target key2, and any key1.
This array may expand when there are a lot of collisions, just like what you do with the ordinary hashmap.