Get index or key given the value in java Map

Given the value, I need to get the index or key which this value belongs in a Map without the need to iterate it. I'm using java.

Thanks


Guava has a BiMap, which is a bidirectional map (each key and each value are unique).

If you don't waht to use an external library, you only have 2 options:

  • use a plain HashMap and you will need to iterate over the keys
  • use 2 hashmaps, one relating keys to values, and one relating values to keys (which in essence is what a bidirectional map does).

  • You could always use a BidiMap from commons-collections:

    http://commons.apache.org/collections/apidocs/org/apache/commons/collections/BidiMap.html


    You have to use some kind of BidirectionalMap . For example this one: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/BiMap.html

    链接地址: http://www.djcxy.com/p/23990.html

    上一篇: 如何检查地图中是否包含密钥?

    下一篇: 获取在java Map中给定值的索引或键