English 中文(简体)
爪哇的双向地图? [重复]
原标题:Bi-directional Map in Java? [duplicate]

我在爪哇有一个简单的整数到字符串映射图,但我需要能够很容易地从整数中检索字符串,也可以从字符串中检索整数。我试过了地图,但它只能从整数中检索字符串,这是唯一的一种方法:

private static final Map<Integer, String> myMap = new HashMap<Integer, String>();
// This works one way:
String myString = myMap.get(myInteger);

// I would need something like:
Integer myInteger = myMap.getKey(myString);

是否有正确的方法 这样做,有它 双向?

另一个问题是,我只有几个不变值,不会改变( 1-gt; "low", 2-gt; "mid", 3 & gt; "high" ,所以不值得选择一个复杂的解决方案 。

最佳回答

您可以为此使用Google 收藏 API, centrely 重新命名为 rel=“noreferr>>Guava , 具体为 BiMap

A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.

问题回答

建立瓜瓦比亚地图并获得其反向价值是微不足道的。

一个简单的例子:

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

public class BiMapTest {

  public static void main(String[] args) {

    BiMap<String, String> biMap = HashBiMap.create();

    biMap.put("k1", "v1");
    biMap.put("k2", "v2");

    System.out.println("k1 = " + biMap.get("k1"));
    System.out.println("v2 = " + biMap.inverse().get("v2"));
  }
}

爪哇标准API中没有双向地图。 您可以自己维护两张地图, 或者使用阿帕奇收藏中的 < a href=" http://commons.pache.org/ proper/commons-countings/apidcs/org/apache/commons/ recoverys4/BidiMap.html" rel="noreferr" > BidiMap 。

您可以在您的地图结构中插入密钥、 价值配对及其反向, 但必须将整数转换为字符串 :

map.put("theKey", "theValue");
map.put("theValue", "theKey");

使用地图. get (“ 价值 ” ) 然后返回“ 钥匙 ” 。

我制作了固定的地图, 它是一个快速和肮脏的方式,我制作了固定的地图, 只能用于一些特定的数据集:

  • Contains only 1 to 1 pairs
  • Set of values is disjoint from the set of keys (1->2, 2->3 breaks it)

如果您想要保留 您可以保留第二个 地图, 以“ put” 值 - & gt; 密钥对 。

使用Google sBiMap

更方便些





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签