### Question:
CycleChecker { public static CheckResult check(Map<String, Object> m) { return check("root", m); } private  CycleChecker(); static CheckResult check(Map<String, Object> m); static CheckResult check(String rootName, Map<String, Object> m);  }### Answer:
@Test public void test1() throws Exception { Map<String, Object> m = new HashMap<>(); Map<String, Object> m1 = new HashMap<>(); Map<String, Object> m2 = new HashMap<>(); m1.put("mm1", m2); m2.put("mm2", m1); m.put("m1", m1); m.put("m2", m2); System.out.println(CycleChecker.check(m)); assertTrue(CycleChecker.check(m).isHasCycle()); }
@Test public void test2() throws Exception { Map<String, Object> m = new HashMap<>(); m.put("m2", Arrays.asList("a1", m)); System.out.println(CycleChecker.check(m)); assertTrue(CycleChecker.check(m).isHasCycle()); }
@Test public void test3() throws Exception { Map<String, Object> m = new HashMap<>(); m.put("k", "v"); m.put("k2", Collections.singletonMap("kk2", "value")); System.out.println(CycleChecker.check(m)); assertFalse(CycleChecker.check(m).isHasCycle()); System.out.println(new ObjectMapper().writeValueAsString(m)); }
@Test public void test4() throws Exception { Map<String, Object> m = new HashMap<>(); m.put("m2", Arrays.asList("a1", Collections.singletonMap("kk2", "value"))); System.out.println(new ObjectMapper().writeValueAsString(m)); System.out.println(CycleChecker.check(m)); assertFalse(CycleChecker.check(m).isHasCycle()); }
@Test public void test5() throws Exception { Map<String, Object> m = new HashMap<>(); Map<String, Object> m1 = new HashMap<>(); Map<String, Object> m2 = new HashMap<>(); m1.put("k1", "v1"); m2.put("k2", "v2"); m2.put("k3", m1); m.put("m1", m1); m.put("m2", m2); System.out.println(new ObjectMapper().writeValueAsString(m)); System.out.println(CycleChecker.check(m)); assertFalse(CycleChecker.check(m).isHasCycle()); }