If I have the value "foo"
, and a HashMap<String> ftw
for which ftw.containsValue("foo")
returns true
, how can I get the corresponding key? Do I have to loop through the hashmap? What is the best way to do that?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
For Android development targeting API < 19, Vitalii Fedorenko one-to-one relationship solution doesn't work because
Objects.equals
isn't implemented. Here's a simple alternative:Some additional info... May be useful to you
Above method may not be good if your hashmap is really big. If your hashmap contain unique key to unique value mapping, you can maintain one more hashmap that contain mapping from Value to Key.
That is you have to maintain two hashmaps
In that case you can use second hashmap to get key.
You can get the key using values using following code..
You can use the below:
There is no unambiguous answer, because multiple keys can map to the same value. If you are enforcing unique-ness with your own code, the best solution is to create a class that uses two Hashmaps to track the mappings in both directions.