0

consider I have a hash map map(two, twooo) and other hash map pun(twooo, 3)

Now i need to get the key "two" from the value "3"

How can I do that?

Punith R Kashi
  • 139
  • 1
  • 10
  • You can't get a key out of a HashMap. You get values using keys that you already know. Also, there are no paths from "3" to "two" using your maps – OneCricketeer Feb 23 '16 at 22:13
  • You just need to indirectly index the second map, using the mapping from first. – Tassos Bassoukos Feb 23 '16 at 22:14
  • 1
    @cricket_007 Yes you can. Internally key-value pairs are stored using an `Entry` object which can be iterated over. Just can't be done in constant time obviously. – Kon Feb 23 '16 at 22:16
  • basically you need to iterate through the second map and when you find the desired key, value pair you save it to another variable and start iterating over the first map and comparing to key, value pair to the one you found from the second map. – Jay T. Feb 23 '16 at 22:17
  • You could use a [bidirectional map](http://stackoverflow.com/questions/10699492/bi-directional-map-in-java) instead of a hashmap, and then just do a reverse lookup. – azurefrog Feb 23 '16 at 22:22

2 Answers2

0

Assuming that you have a different value for each key, you can do something like this:

private Map<String, Integer> team1 = new HashMap<String, Integer>();

  team1.put("1", 1);
  team1.put("2", 2)


private String getKey(Integer value){
   for(String key : team1.keySet()){
     if(team1.get(key).equals(value)){
        return key; //return the first found
     }
  }
  return null;
}

In same manner iterate through other hash map to find a key of value which is key in other hashMap.

Waqas Ahmed
  • 1,780
  • 1
  • 14
  • 15
0

If you're using HashMap, this will be very inefficient, you have to loop over the values in both maps to find the key you're looking for, something like:

public static <K1, VK, V2> K1 findKeyInFirstMap(
    Map<K1, VK> map1, Map<VK, V2> map2, V2 value) {
  VK valueKey = null;
  for (Entry<VK, V2> e : map2.entrySet()) {
    if (e.getValue().equals(value)) {
      valueKey = e.getKey();
      break;
    }
  }
  if (valueKey == null) {
    throw new IllegalArgumentException(value + " not found in map2");
  }
  for (Entry<K1, VK> e : map1.entrySet()) {
    if (e.getValue().equals(valueKey)) {
      return e.getKey();
    }
  }
  throw new IllegalArgumentException(valueKey + " not found in map1");
}

This assumes your maps don't have null keys or values, but why would you do that to yourself? :)

Much more efficient would be to use a bi-directional mapping, like Guava's BiMap. With a BiMap, it's much simpler, and O(1) instead of O(n):

map1.inverse().get(map2.inverse().get(value));

Or, with the same error semantics as the previous method:

public static <K1, VK, V2> K1 findKeyInFirstMap(
        BiMap<K1, VK> map1, BiMap<VK, V2> map2, V2 value) {
  VK valueKey = map2.inverse().get(value);
  if (valueKey == null) {
    throw new IllegalArgumentException(value + " not found in map2");
  }
  K1 key = map1.inverse().get(valueKey);
  if (key == null) {
    throw new IllegalArgumentException(valueKey + " not found in map1");
  }
  return key;
}
dimo414
  • 47,227
  • 18
  • 148
  • 244