Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you choose to use the <a href="http://commons.apache.org/collections/" rel="noreferrer">Commons Collections library</a> instead of the standard Java Collections API, you can achieve this with ease.</p> <p>The <a href="http://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/BidiMap.html" rel="noreferrer">BidiMap</a> 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. Obtaining a key for a value is supported by the <a href="http://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/BidiMap.html#getKey%28java.lang.Object%29" rel="noreferrer">getKey() method</a>.</p> <p>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.</p> <p><strong>Update</strong></p> <p>If you want to rely on the Java Collections API, you will have to ensure the 1:1 relationship between keys and values at the time of inserting the value into the map. This is easier said than done.</p> <p>Once you can ensure that, use the <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#entrySet()" rel="noreferrer">entrySet() method</a> to obtain the set of entries (mappings) in the Map. Once you have obtained the set whose type is <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html" rel="noreferrer">Map.Entry</a>, iterate through the entries, comparing the <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html#getValue()" rel="noreferrer">stored value</a> against the expected, and obtain the <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html#getKey()" rel="noreferrer">corresponding key</a>.</p> <p><strong>Update #2</strong></p> <p>Support for bidi maps with generics can be found in <a href="https://github.com/google/guava" rel="noreferrer">Google Guava</a> and the refactored <a href="http://sourceforge.net/projects/collections/" rel="noreferrer">Commons-Collections</a> libraries (the latter is not an Apache project). Thanks to Esko for pointing out the missing generic support in Apache Commons Collections. Using collections with generics makes more maintainable code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload