用java中的二维键映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6373443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:26:06  来源:igfitidea点击:

Map with two-dimensional key in java

javahashmap

提问by EclipseQuestion

I want a map indexed by two keys (a map in which you put AND retrieve values using two keys) in Java. Just to be clear, I'm looking for the following behavior:

我想要一个由 Java 中的两个键(使用两个键放置和检索值的映射)索引的地图。为了清楚起见,我正在寻找以下行为:

map.put(key1, key2, value); 
map.get(key1, key2); // returns value
map.get(key2, key1); // returns null
map.get(key1, key1); // returns null

What's the best way to to it? More specifically, should I use:

最好的方法是什么?更具体地说,我应该使用:

  • Map<K1,Map<K2,V>>

  • Map<Pair<K1,K2>, V>

  • Other?

  • Map<K1,Map<K2,V>>

  • Map<Pair<K1,K2>, V>

  • 其他?

(where K1,K2,V are the types of first key, second key and value respectively)

(其中 K1,K2,V 分别是第一键、第二键和值的类型)

采纳答案by Edwin Buck

You should use Map<Pair<K1,K2>, V>

你应该使用 Map<Pair<K1,K2>, V>

  1. It will only contain one map, instead of N+1 maps

  2. Key construction will be obvious (creation of the Pair)

  3. Nobody will get confused as to the meaning of the Map as its programmer facing API won't have changed.

  4. Dwell time in the data structure would be shorter, which is good if you find you need to synchronize it later.

  1. 它将只包含一张地图,而不是 N+1 张地图

  2. 关键构建将是显而易见的(Pair 的创建)

  3. 没有人会对 Map 的含义感到困惑,因为它面向程序员的 API 不会改变。

  4. 数据结构中的停留时间会更短,如果您发现稍后需要同步它,这很好。

回答by Waldheinz

I would opt for the Map<Pair<K1,K2>, V>solution, because:

我会选择Map<Pair<K1,K2>, V>解决方案,因为:

  • it directly expresses what you want to do
  • is potentially faster because it uses fewer indirections
  • simplifies the client code (the code that uses the Mapafterwards
  • 它直接表达了你想做什么
  • 可能更快,因为它使用更少的间接
  • 简化客户端代码(使用Map之后的代码

回答by Jaime

I'd recommend going for the second option

我建议去第二个选择

Map<Pair<K1,K2>,V>

The first one will generate more overload when retrieving data, and even more when inserting/removing data from the Map. Every time that you put a new Value V, you'll need to check if the Map for K1 exists, if not create it and put it inside the main Map, and then put the value with K2.

第一个在检索数据时会产生更多的过载,在从 Map 插入/删除数据时会产生更多的过载。每次放置一个新的Value V 时,您需要检查K1 的Map 是否存在,如果不存在,则创建它并将其放入主Map 中,然后将值与K2 一起放置。

If you want to have an interface as you're exposing initially wrap your Map<Pair<K1,K2>,V>with your own "DoubleKeyMap".

如果您想在最初公开时拥有一个界面,请Map<Pair<K1,K2>,V>用您自己的“DoubleKeyMap”包装您。

(And don't forget to properly implement the methods hash and equals in the Pair class!!)

(并且不要忘记在 Pair 类中正确实现方法 hash 和 equals !!)

回答by Jean Logeart

Logically, you Pair (key1, key2) corresponds to somethingsince it is the key of your map. Therefore you may consider writing your own class having K1 and K2 as parameters and overriding the hashCode() method (plus maybe other methods for more convenience). This clearly appears to be a "clean" way to solve your problem.

从逻辑上讲,您 Pair (key1, key2) 对应于某些东西,因为它是您地图的键。因此,您可以考虑编写自己的类,将 K1 和 K2 作为参数并覆盖 hashCode() 方法(可能还有其他更方便的方法)。这显然是解决问题的“干净”方式。

回答by Dave

If you're willing to bring in a new library (which I recommend), take a look at Tablein Guava. This essentially does exactly what you're looking for, also possibly adding some functionality where you may want all of the entries that match one of your two keys.

如果您愿意引入一个新库(我推荐),请查看Tablein Guava。这基本上完全符合您的要求,还可能添加一些功能,您可能需要与您的两个键之一匹配的所有条目。

interface Table<R,C,V>

A collection that associates an ordered pair of keys, called a row key and a column key, with a single value. A table may be sparse, with only a small fraction of row key / column key pairs possessing a corresponding value.

将一对有序键(称为行键和列键)与单个值相关联的集合。一个表可能是稀疏的,只有一小部分行键/列键对拥有相应的值。

回答by pseudoramble

While I also am on board with what you proposed (a pair of values to use as the key), you could also consider making a wrapper which can hold/match both keys. This might get somewhat confusing since you would need to override the equals and hashCode methods and make that work, but it could be a straightforward way of indicating to the next person using your code that the key must be of a special type.

虽然我也同意你的建议(一对用作键的值),但你也可以考虑制作一个可以保存/匹配两个键的包装器。这可能会有些混乱,因为您需要覆盖 equals 和 hashCode 方法并使其工作,但这可能是一种直接的方式,可以向使用您的代码的下一个人表明该键必须是特殊类型。

Searching a little bit, I found this postwhich may be of use to you. In particular, out of the Apache Commons Collection, MultiKeyMap. I've never used this before, but it looks like a decent solution and may be worth exploring.

稍微搜索了一下,我发现这篇文章可能对你有用。特别是,在 Apache Commons Collection 之外,MultiKeyMap。我以前从未使用过它,但它看起来是一个不错的解决方案,可能值得探索。

回答by sre

I have used array for the key: like this

我已经使用数组作为键:像这样

Map<Array[K1,K2], V>

Map<Array[K1,K2], V>