简体   繁体   English

如何基于Map Interface Java中的值检索键?

[英]How to retrieve a key based on the value in Map Interface Java?

I am currently working on Data Structures for writing a program on encryption and decryption of names. 我目前正在研究数据结构,以编写有关名称加密和解密的程序。 I have a doubt in Map interface. 我对Map界面有疑问。 Actually to get the value associated with a key we have get() method in Map interface. 实际上,要获取与键关联的值,我们在Map接口中具有get()方法。 But how to retrieve the key of a particular value without iterating through all the key value pairs in Map interface 但是,如何在不迭代Map接口中所有键值对的情况下检索特定值的键

Thank you 谢谢

how to retrieve the key of a particular value without iterating through all the key value pairs in Map interface 如何检索特定值的键而不迭代Map界面中的所有键值对

Key is Key, not the value . 键是键,而不是值 You cannot do it. 你做不到。 That's not how Map implemented. 那不是Map的实现方式。

Even If you make it with some magic ( iterating multiple times, checking for equls etc .. ), that's not guaranteed to give expected result.. 即使您使用某种魔术方法( 多次迭代,检查是否有equal等.. ),也不能保证得到预期的结果。

And as per the definition of Map, Key is unique not the value. 根据Map的定义,Key是唯一的,而不是值。 So there will be duplicated values and when you get by value, which associated key you will expect to get ? 因此,会有重复的值,当您按值获取时,您期望获得哪个关联的键?

If you are sure that there are no duplicates, you can do 如果您确定没有重复项,则可以

 for (Entry<Integer, String> entry : testMap.entrySet()) {
            if (entry.getValue().equals("c")) {
                System.out.println(entry.getKey());
            }
        }

Given that values are unique, you could to it like this: 鉴于值是唯一的,您可以这样:

    Map<String, String> map = new HashMap<>();
    map.put("key1", "value1");
    map.put("key2", "value2");

    String key = map.entrySet().stream().
    collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))
    .get("value1");

    System.out.println(key); //gives key1

As others have said, it can't be done. 正如其他人所说,这是无法完成的。 The Map interface and its implementations do not support that. Map接口及其实现不支持此功能。

Consider using a BiMap such as the one inculed in Google Guava Collections. 考虑使用BiMap例如在Google Guava Collections中灌装的BiMap。 It establishes a one-to-one (bidirectional) relationship between keys and values. 它在键和值之间建立一对一(双向)关系。 https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap

Using BiMap you can use Key key = biMap.inverse().get(value) to get a key for a given value. 使用BiMap您可以使用Key key = biMap.inverse().get(value)来获取给定值的键。

您不能这样做,因为“值”可以重复。

Well like everyone said, you can't really do it in a decent way because there can be duplicate values. 就像每个人都说的那样,您不可能真正做到体面的方式,因为可能存在重复的值。 You could search for a hit using the equals() method and compare values. 您可以使用equals()方法搜索匹配并比较值。 but then again, why are you even using a key/value map if you wanted to do such a thing. 但是再说一次,如果您想这样做,为什么还要使用键/值映射呢?

in short, you could write your own version of the get method, taking in a instance of the value object you are trying to get. 简而言之,您可以编写自己的get方法版本,并获取要获取的value对象的实例。 But there really is not a point in using a map if you are going to do that. 但是,如果要这样做,使用地图确实没有意义。

Why not use a list of some sorts of you desire to search on value ? 为什么不使用一些您想要搜索价值的清单?

As said, that is not provided by Java-Map interface, since you should have a key and get the value then. 如前所述,这不是Java-Map接口提供的,因为您应该有一个键并获取值。

Usually you have something like this. 通常你有这样的事情。

User user = ...;
HashMap<String, User> usernamesToUser = ...

Then you can get the key by something like: 然后,您可以通过以下方式获取密钥:

String username = user.getUsername();

So without using the map actually. 因此无需实际使用地图。 However what you can do is, if the key is not directly to retrieve from the object you can use two Maps for both directions. 但是,您可以做的是,如果关键不是直接从对象中检索,则可以对两个方向使用两个“地图”。 So consider former example (just assume User the User object does not safe the Username) 因此,请考虑前面的示例(假设用户User对象不保护用户名)

Map<User, String> userMapReverse = ....;
Map<String, User> userMap = ....;
String username = userMapReverse.get(user);

However this option requires that you maintain two maps, which can be pretty ugly sometimes. 但是,此选项需要您维护两张地图,有时这有时很难看。

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

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