9 Replies Latest reply: Jul 3, 2008 10:02 AM by 843785 RSS

    HASH MAP ??

    843785
      HOW TO GET THE KEY, BY GIVING THE VALUE USING A HASHMAP OBJECT??
        • 1. Re: HASH MAP ??
          800282
          ._   _  ____ _______   _____   ____   _____ _____ _____ ____  _      ______ 
          | \ | |/ __ \__   __| |  __ \ / __ \ / ____/ ____|_   _|  _ \| |    |  ____|
          |  \| | |  | | | |    | |__) | |  | | (___| (___   | | | |_) | |    | |__   
          | . ` | |  | | | |    |  ___/| |  | |\___ \\___ \  | | |  _ <| |    |  __|  
          | |\  | |__| | | |    | |    | |__| |____) |___) |_| |_| |_) | |____| |____ 
          |_| \_|\____/  |_|    |_|     \____/|_____/_____/|_____|____/|______|______|
                                                                                      
          • 2. Re: HASH MAP ??
            843785
            AMARSHI wrote:
            HOW TO GET THE KEY, BY GIVING THE VALUE USING A HASHMAP OBJECT??
            If you change this to "how do I get *A* key, by giving the value using a HashMap," this becomes possible, but also indicates that you're not using the map in an appropriate manner. You could have a Map with 2000 unique keys that all point to the same value, after all.
            //returns the FIRST key (using the KeySet's possibly arbitrary iterator) that maps to value
            public <K, V> K getKey( Map<K, V> myMap, Object value ) {
               for ( Map.Entry<K, V> entry : myMap ) {
                  if (value.equals(entry.getValue())) {
                     return entry.getKey();
                  }
               }
            }
            • 3. Re: HASH MAP ??
              796254
              Use Google's multi-map. You might be able to get the List of keys for a given value that way. I haven't looked at the API in detail.
              • 4. Re: HASH MAP ??
                843785
                Map map = new HashMap();
                Object key1 = "key1";
                Object key2 = "key2";
                Object value = "value";
                map.put(key1, value);
                map.put(key2, value);
                What is the key for 'value' above?
                • 5. Re: HASH MAP ??
                  843785
                  [http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/BidiMap.html]

                  ~
                  • 6. Re: HASH MAP ??
                    843785
                    AMARSHI wrote:
                    HOW TO GET THE KEY, BY GIVING THE VALUE USING A HASHMAP OBJECT??
                    You take out an entry set of the HashMap (using the entrySet method). This set will consist of Map.Entry objects which are the key/value pairs of the HashMap. You then iterate the set looking for key/value pairs with the wanted value (there can be more than one, and none of course). Once you find a value you also have the key.

                    The above search for value will be O(N) and much slower than a normal key search which is O(1).
                    • 7. Re: HASH MAP ??
                      843785
                      georgemc wrote:
                      Map map = new HashMap();
                      Object key1 = "key1";
                      Object key2 = "key2";
                      Object value = "value";
                      map.put(key1, value);
                      map.put(key2, value);
                      What is the key for 'value' above?
                      OP, if you don't know what you have, that is, if you think you might have a situation similar to the one george shows above, print out the contents of your map.
                      private void printOutMap(java.util.Map m)
                      {
                         java.util.Iterator iter = m.entrySet().iterator();
                         while(iter.hasNext())
                         {
                            Map.Entry entry = (Map.Entry) iter.next();
                            Object key = entry.getKey();
                            Object value = entry.getValue();
                            System.out.println(key+":"+value);
                         }
                      }
                      • 8. Re: HASH MAP ??
                        843785
                        Thnx a lotttttttttttt...............
                        i never knew that such an interface exist that has a bidirectional lookup....

                        bye tk kr....
                        • 9. Re: HASH MAP ??
                          843785
                          Thnx a lotttttttttttt...............
                          Stutter?

                          ~