简体   繁体   English

Java Map从价值中获取关键

[英]Java Map get key from value

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` 

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. 与从键中获取值相比,从Hashtable或HashMap中获取值并不容易,因为Hash Map或Hashtable不会在Java中的Map内部在键与值之间实现一对一的映射。 infact Map allows same value to be mapped against multiple keys inside HashMap, Hashtable or any other Map implementation. 实际上,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
            }
        }

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 ). 但是,如果一定不要运行循环,那么Google的Guava库中有一个名为BiMap的BiDirectional Map Collection,可以在此处找到 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. 否则,重复的值将没有链接的特定键。

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. 在大多数情况下,循环通过将可以正常工作。

Taken from this SO answer 这个SO答案

If you choose to use the Commons Collections library instead of the standard Java Collections API, you can achieve this with ease. 如果您选择使用Commons Collections库而不是标准Java Collections API,则可以轻松实现此目的。

The BidiMap interface 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. Collections库中的BidiMap接口是双向映射,允许您将键映射到值(如法线映射),也可以将值映射到键,从而允许您在两个方向上执行查找。 Obtaining a key for a value is supported by the getKey() method . getKey()方法支持获取值的键。

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. 需要注意的是,bidi映射不能将多个值映射到键,因此,除非您的数据集在键和值之间具有1:1映射,否则您不能使用bidimaps。

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循环来检查值和获取键,并决定在值匹配时中断或继续操作。

If you're sure that your values are unique you can iterate over the entries of your old 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM