0

Say I have a map such as:

{0='.', 1='.', 2='.', 3='.', 4='.', 5='.', 6='G'}

How do I find the key which matches the 'G' value? Say I wanted to print the key for which 'G' is equal to. All help appreciated

  • With a regular `Map`, you would have to iterate through all entries and return the key for the entry whose value matches yours. – Sotirios Delimanolis Feb 24 '14 at 05:04
  • 2
    What do you want for `.`? – Bhesh Gurung Feb 24 '14 at 05:04
  • Your question is very unclear. There's no 1-to-1 mapping from values to keys. As @BheshGurung noted, what would you want to return if you looked for `'.'` instead of `'G'`? – Jim Garrison Feb 24 '14 at 05:08
  • @JimGarrison If looking for '.' would want it to return the first key which matches the value, in this case 0. –  Feb 24 '14 at 05:11
  • @pokeairguy: if you use common collection API then you will have to traverse the map and then find the key for value. But there is another API that is BidiMap interface in the Collections library (provided by org.apache), 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. This Interface you can found in org.apache.commons.collections package. – Suneet Bansal Feb 24 '14 at 05:22

0 Answers0