fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
  11.  
  12. for (Entry<T, E> entry : map.entrySet()) {
  13.  
  14. if (value.equals(entry.getValue())) {
  15. return entry.getKey();
  16. }
  17. }
  18.  
  19. return null;
  20. }
  21.  
  22. public static void main (String[] args) throws java.lang.Exception
  23. {
  24. Map<String,String> myMap = new HashMap<String,String>();
  25. myMap.put("1","valor1");
  26. myMap.put("2","valor2");
  27. myMap.put("3","valor3");
  28.  
  29. System.out.println(getKeyByValue(myMap,"valor1"));
  30. }
  31. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
1