CollectionMethod { public static <K, V> Map<K, V> recursive(Map<K, V> map) { Map<K, V> result = new HashMap<K, V>(); _recursive(result, map); return result; } private  CollectionMethod(); static ListCycle<T> toCycle(Collection<T> values); static ArrayCycle<T> toCycle(T[] values); static BooleanArrayCycle toCycle(boolean[] values); static CharArrayCycle toCycle(char[] values); static ByteArrayCycle toCycle(byte[] values); static ShortArrayCycle toCycle(short[] values); static IntArrayCycle toCycle(int[] values); static LongArrayCycle toCycle(long[] values); static FloatArrayCycle toCycle(float[] values); static DoubleArrayCycle toCycle(double[] values); static int length(Map<?, ?> values); static int length(Collection<?> values); static int length(Object[] values); static int length(boolean[] values); static int length(char[] values); static int length(byte[] values); static int length(short[] values); static int length(int[] values); static int length(long[] values); static int length(float[] values); static int length(double[] values); static Map<K, V> sort(Map<K, V> map); @SuppressWarnings({"unchecked", "rawtypes"}) static List<T> sort(List<T> list); static Set<T> sort(Set<T> set); static Collection<T> sort(Collection<T> set); static T[] sort(T[] array); static char[] sort(char[] array); static byte[] sort(byte[] array); static short[] sort(short[] array); static int[] sort(int[] array); static long[] sort(long[] array); static float[] sort(float[] array); static double[] sort(double[] array); static Map<K, V> recursive(Map<K, V> map); static List<T> recursive(Collection<T> set); static List<T> recursive(Collection<T> set, String children); static List<T> recursive(T node, String children);  }
@Test public void testRecursiveObject() throws Exception { List<Menu> children = new ArrayList<Menu>(); children.add(new Menu("x")); children.add(new Menu("y")); children.add(new Menu("z")); List<Menu> parent = new ArrayList<Menu>(); parent.add(new Menu("a").setChildren(children)); parent.add(new Menu("b")); parent.add(new Menu("c")); List<Menu> result = CollectionMethod.recursive(parent, "getChildren"); Assert.assertEquals(6, result.size()); Assert.assertEquals("a", result.get(0).getName()); Assert.assertEquals("x", result.get(1).getName()); Assert.assertEquals("y", result.get(2).getName()); Assert.assertEquals("z", result.get(3).getName()); Assert.assertEquals("b", result.get(4).getName()); Assert.assertEquals("c", result.get(5).getName()); }
@Test public void testRecursiveList() throws Exception { List<Object> children = new ArrayList<Object>(); children.add("x"); children.add("y"); children.add("z"); List<Object> parent = new ArrayList<Object>(); parent.add("a"); parent.add(children); parent.add("b"); parent.add("c"); List<Object> result = CollectionMethod.recursive(parent); Assert.assertEquals(6, result.size()); Assert.assertEquals("a", result.get(0)); Assert.assertEquals("x", result.get(1)); Assert.assertEquals("y", result.get(2)); Assert.assertEquals("z", result.get(3)); Assert.assertEquals("b", result.get(4)); Assert.assertEquals("c", result.get(5)); }
@Test public void testRecursiveMap() throws Exception { Map<String, Object> children = new HashMap<String, Object>(); children.put("4", "x"); children.put("5", "y"); children.put("6", "z"); Map<String, Object> parent = new HashMap<String, Object>(); parent.put("1", "a"); parent.put("children", children); parent.put("2", "b"); parent.put("3", "c"); Map<String, Object> result = CollectionMethod.recursive(parent); Assert.assertEquals(6, result.size()); Assert.assertEquals("a", result.get("1")); Assert.assertEquals("x", result.get("4")); Assert.assertEquals("y", result.get("5")); Assert.assertEquals("z", result.get("6")); Assert.assertEquals("b", result.get("2")); Assert.assertEquals("c", result.get("3")); }