一张地图有两个钥匙

发布于 2024-12-06 15:22:43 字数 493 浏览 13 评论 0 原文

可能的重复:
如何实现具有多个键的 Map?
多个键到单值映射 Java

我必须获取的值基于两个传入字符串属性的枚举。我一直将其作为单个值的地图。现在我面临着串联。有没有办法让地图有两个键,这样我

Map.get("attr1","attr2");

就可以返回正确的枚举。或者我只需连接所有可能的值并将其用作键?

我正在寻找非常干净的解决方案(我们不是吗:P)

Possible Duplicate:
How to implement a Map with multiple keys?
Multiple Keys to Single Value Map Java

I have to get the value of an enum based on two incoming string attributes. I have been doing this as map for single values. Now I am faced with making a concatenation. Is there a way to have a map have two keys such that I could

Map.get("attr1","attr2");

and this would return the correct enum. Or will I simply have to concatenate all possible values and use this as the key?

I'm searching for the squeaky clean solution (aren't we all :P)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雅心素梦 2024-12-13 15:22:43

好吧,您可以使用 Map>

我们经常使用它,因此制作了自己的 Map 实现,它提供两个键并在内部使用 map-of-maps 方法。

Well, you could use Map<String, Map<String, YourEnum>>.

We use this a lot and thus made our own Map implementation that provides two keys and internally uses the map-of-maps approach.

二智少女猫性小仙女 2024-12-13 15:22:43

您可以有一个地图的地图,即,

Map.get("attr1").get("attr2");

或者您可以使用 equals 方法定义一个对象,即

Map.get(new MyObject("attr1", "attr2"));

You could have a map of maps, i.e.

Map.get("attr1").get("attr2");

or you could define a object with an equals method, i.e.

Map.get(new MyObject("attr1", "attr2"));
绝不服输 2024-12-13 15:22:43

在这种情况下,我将创建一个具有这两个属性的包装类,实现 equals() 并可能实现 ComparablecompareTo 委托给这两个元素的复合键。并使用此类作为地图的键。

这就像滚动您自己的 Pair 类。看看这个问题的答案,详细说明了这个问题:Java 中的 C++ Pair 相当于什么?

In this case I would make a wrapper class with those two attributes, implement equals() and maybe implement Comparable and compareTo delegating onto the two elements of the composite key. And use this class as key to the map.

It would be like rolling your own Pair class. Take a look at this question's answers, that elaborate on the matter: What is the equivalent of the C++ Pair in Java?

依 靠 2024-12-13 15:22:43

为什么不创建一个包含两个键的类并将其用作映射键?

例如:

public class BiKey implements comparable<BiKey>
{
  private String key1;
  private String key2;

  // getters and setters

  // equals and hash code.

  // implement comparable.
}

Map <BiKey, Blammy> blammyMap; // Blammy is the value type.

Why not create an class that contains the two keys and use that as the map key?

For example:

public class BiKey implements comparable<BiKey>
{
  private String key1;
  private String key2;

  // getters and setters

  // equals and hash code.

  // implement comparable.
}

Map <BiKey, Blammy> blammyMap; // Blammy is the value type.
小…红帽 2024-12-13 15:22:43

与托马斯的方法类似,我们(在我的工作中)有“维度”地图类,它们是多个地图的简单包装器。

public class MultiKeyMap {
  private HashMap<keyOne, Object> keyOneToObjectMap;
  private HashMap<keyTwo, Object> keyTwoToObjectMap;
  ...etc


}

保持所有地图彼此同步非常容易,并且无需大量可能复杂的代码即可进行相当快速的搜索。 (但是,这是以增加内存使用为代价的)

Similar to Thomas's approach, we (At my work) have 'Dimensional' map classes that are simple wrappers around several Maps.

public class MultiKeyMap {
  private HashMap<keyOne, Object> keyOneToObjectMap;
  private HashMap<keyTwo, Object> keyTwoToObjectMap;
  ...etc


}

It is very easy to keep all the maps in sync with each other and allows for a decently quick search without tons of potentially complex code. (However, this comes at the cost of increased Memory use)

清风无影 2024-12-13 15:22:43

org.apache.commons.collections.map.MultiKeyMap 就是你想要的

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/MultiKeyMap.html

示例

 private MultiKeyMap cache = MultiKeyMap.decorate(new LRUMap(50));

 public String getAirlineName(String code, String locale) {
   String name = (String) cache.get(code, locale);
   if (name == null) {
     name = getAirlineNameFromDB(code, locale);
     cache.put(code, locale, name);
   }
   return name;
 }

org.apache.commons.collections.map.MultiKeyMap is what you want

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/MultiKeyMap.html

example

 private MultiKeyMap cache = MultiKeyMap.decorate(new LRUMap(50));

 public String getAirlineName(String code, String locale) {
   String name = (String) cache.get(code, locale);
   if (name == null) {
     name = getAirlineNameFromDB(code, locale);
     cache.put(code, locale, name);
   }
   return name;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文