Java Map 从值中获取键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35763137/
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
Java Map get key from value
提问by Your Friend
below is my code...
下面是我的代码...
Map<Integer, String> MyType = sessionInfo.getType();
//{2=somename}
I am trying to get key from value...without running any loops....is it possible?
我正在尝试从值中获取密钥......没有运行任何循环......有可能吗?
MyType.get("somename") // should output 2`
采纳答案by Khan Abdulrehman
It's not easy to get key from value in Hashtable or HashMap, as compared to getting value from key, because Hash Map or Hashtable doesn't enforce one to one mapping between key and value inside Map in Java. infact Map allows same value to be mapped against multiple keys inside HashMap, Hashtable or any other Map implementation.
与从键中获取值相比,从 Hashtable 或 HashMap 中的值获取键并不容易,因为 Hash Map 或 Hashtable 在 Java 中的 Map 中并没有强制执行键和值之间的一对一映射。事实上 Map 允许将相同的值映射到 HashMap、Hashtable 或任何其他 Map 实现中的多个键。
String key= null;
String value="somename";
for(Map.Entry entry: MyType.entrySet()){
if(value.equals(entry.getValue())){
key = entry.getKey();
break; //breaking because its one to one map
}
}
回答by Pat Myron
I would encourage running a loop for simplicity. It most likely will not slow down your program a noticeable amount.
为了简单起见,我鼓励运行一个循环。它很可能不会显着降低您的程序速度。
However, if you must not run a loop, Google's Guava library has a BiDirectional Map Collection called BiMap that can be (found here). The map works both ways and is guaranteed to be synchronized at all times. I also am assuming that you have unique values in your map. If you do not, duplicate values will not have a specific key to link to.
但是,如果您不能运行循环,Google 的 Guava 库有一个名为 BiMap 的双向地图集合,可以(在此处找到)。地图双向工作,并保证始终同步。我还假设您的地图中具有唯一值。如果不这样做,重复值将没有可链接到的特定键。
BiMap<String, Integer> biMapInversed = biMap.inverse(); // how to get inverted map
Again, I wouldn't encourage this unless absolutely necessary. Looping through will work perfectly fine in most cases.
同样,除非绝对必要,否则我不会鼓励这样做。在大多数情况下,循环将工作得很好。
回答by melson.jao
This is not possible. You need to consider the value may be duplicated in map. Ex, How do you deal with {2=somename} and {5=somename}
这不可能。您需要考虑该值可能在地图中重复。例如,你如何处理 {2=somename} 和 {5=somename}
You still need to use a for loop to check value and get key and decide to break or go on when value is matched.
您仍然需要使用 for 循环来检查值并获取键,并在值匹配时决定中断或继续。
回答by kovit nisar
If you're sure that your values are unique you can iterate over the entries of your old map .
如果您确定您的值是唯一的,您可以迭代旧 map 的条目。
Map<String, Character> myNewHashMap = new HashMap<>();
for(Map.Entry<Character, String> entry : myHashMap.entrySet()){
myNewHashMap.put(entry.getValue(), entry.getKey());
}
Alternatively, you can use a Bi-Directional map like Guava provides and use the inverse() method :
或者,您可以使用像 Guava 提供的双向地图并使用 inverse() 方法:
BiMap<Character, String> myBiMap = HashBiMap.create();
myBiMap.put('a', "test one");
myBiMap.put('b', "test two");
BiMap<String, Character> myBiMapInversed = myBiMap.inverse();
回答by Sна?ош?а?
Taken from this SO answer
取自这个 SO 答案
If you choose to use the Commons Collections libraryinstead of the standard Java Collections API, you can achieve this with ease.
The BidiMapinterface in the Collections library is a bi-directional map, allowing you to map a key to a value (like normal maps), and also to map a value to a key, thus allowing you to perform lookups in both directions. Obtaining a key for a value is supported by the getKey() method.
There is a caveat though, bidi maps cannot have multiple values mapped to keys, and hence unless your data set has 1:1 mappings between keys and values, you cannot use bidimaps.
如果您选择使用Commons Collections 库而不是标准的 Java Collections API,则可以轻松实现这一点。
Collections 库中的BidiMap接口是一个双向映射,允许您将键映射到值(如法线映射),也可以将值映射到键,从而允许您在两个方向上执行查找。getKey() 方法支持获取值的键。
但是有一个警告,bidi 映射不能将多个值映射到键,因此除非您的数据集在键和值之间具有 1:1 映射,否则您不能使用 bidimap。