fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Map<String, String> dataset = new HashMap<String,String>(){{
  13. put("def","abc");
  14. put("ghi", "def");
  15. put("jkl","ghi");
  16. put("mno","ghi");
  17. put("stu","pqr");
  18. }};
  19. // your code goes here
  20. factor_transitivity(dataset, false);
  21. System.out.println(dataset);
  22. }
  23. private static void factor_transitivity(Map<String,String> dataset, boolean complete) {
  24. if (!complete) {
  25. complete = true; // Assume that on this check everything will come out to be OK.
  26. for (Map.Entry<String, String> pair : dataset.entrySet()) {
  27. if (dataset.containsKey(pair.getValue())) {
  28. complete = false;
  29. String value = dataset.get(pair.getValue());
  30. dataset.put(pair.getKey(),value);
  31. System.out.print("Found transitive key "+pair.getKey());
  32. break;
  33. }
  34. }
  35. factor_transitivity(dataset, complete);
  36. }
  37. }
  38. }
Success #stdin #stdout 0.17s 57436KB
stdin
Standard input is empty
stdout
Found transitive key ghiFound transitive key jklFound transitive key mno{def=abc, stu=pqr, ghi=abc, jkl=abc, mno=abc}