/* package whatever; // don't place package name! */
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
for (Entry<T, E> entry : map.entrySet()) {
if (value.equals(entry.getValue())) {
return entry.getKey();
}
}
return null;
}
{
myMap.put("1","valor1");
myMap.put("2","valor2");
myMap.put("3","valor3");
System.
out.
println(getKeyByValue
(myMap,
"valor1")); }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC5IYXNoTWFwOwppbXBvcnQgamF2YS51dGlsLk1hcDsKaW1wb3J0IGphdmEudXRpbC5NYXAuRW50cnk7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgPFQsIEU+IFQgZ2V0S2V5QnlWYWx1ZShNYXA8VCwgRT4gbWFwLCBFIHZhbHVlKSB7CgkKCSAgICBmb3IgKEVudHJ5PFQsIEU+IGVudHJ5IDogbWFwLmVudHJ5U2V0KCkpIHsKCQoJICAgICAgICBpZiAodmFsdWUuZXF1YWxzKGVudHJ5LmdldFZhbHVlKCkpKSB7CgkgICAgICAgICAgICByZXR1cm4gZW50cnkuZ2V0S2V5KCk7CgkgICAgICAgIH0KCSAgICB9CgkKCSAgICByZXR1cm4gbnVsbDsKCX0KCQoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4gKFN0cmluZ1tdIGFyZ3MpIHRocm93cyBqYXZhLmxhbmcuRXhjZXB0aW9uCgl7CgkJTWFwPFN0cmluZyxTdHJpbmc+IG15TWFwID0gbmV3IEhhc2hNYXA8U3RyaW5nLFN0cmluZz4oKTsKCQlteU1hcC5wdXQoIjEiLCJ2YWxvcjEiKTsKCQlteU1hcC5wdXQoIjIiLCJ2YWxvcjIiKTsKCQlteU1hcC5wdXQoIjMiLCJ2YWxvcjMiKTsKCQkKCQlTeXN0ZW0ub3V0LnByaW50bG4oZ2V0S2V5QnlWYWx1ZShteU1hcCwidmFsb3IxIikpOwoJfQp9