@Test public void testJoinWithFunction() { final Collection.Function<Integer, String> parityFunction = x -> x % 2 == 0 ? "even" : "odd"; final ArrayList<Integer> list = new ArrayList<>(); assertEquals("", Collection.join(list, ",", parityFunction)); list.add(1); assertEquals("odd", Collection.join(list, ",", parityFunction)); list.add(2); assertEquals("odd,even", Collection.join(list, ",", parityFunction)); assertEquals("odd:sep:even", Collection.join(list, ":sep:", parityFunction)); assertEquals("oddeven", Collection.join(list, "", parityFunction)); }
public static <T> String join(List<T> list, String separator, Function<T, String> function) { String result = ""; for (int i = 0; i < list.size(); i++) { result += function.apply(list.get(i)).trim(); if (i + 1 < list.size()) { result += separator; } } return result; }
Collection { public static <T> String join(List<T> list, String separator, Function<T, String> function) { String result = ""; for (int i = 0; i < list.size(); i++) { result += function.apply(list.get(i)).trim(); if (i + 1 < list.size()) { result += separator; } } return result; } }
Collection { public static <T> String join(List<T> list, String separator, Function<T, String> function) { String result = ""; for (int i = 0; i < list.size(); i++) { result += function.apply(list.get(i)).trim(); if (i + 1 < list.size()) { result += separator; } } return result; } private  Collection(); }
Collection { public static <T> String join(List<T> list, String separator, Function<T, String> function) { String result = ""; for (int i = 0; i < list.size(); i++) { result += function.apply(list.get(i)).trim(); if (i + 1 < list.size()) { result += separator; } } return result; } private  Collection(); static String[] tail(String[] args); @SafeVarargs static T[] create(T... args); static String join(List<T> list, String separator, Function<T, String> function); static String join(List<String> list, String separator); }
Collection { public static <T> String join(List<T> list, String separator, Function<T, String> function) { String result = ""; for (int i = 0; i < list.size(); i++) { result += function.apply(list.get(i)).trim(); if (i + 1 < list.size()) { result += separator; } } return result; } private  Collection(); static String[] tail(String[] args); @SafeVarargs static T[] create(T... args); static String join(List<T> list, String separator, Function<T, String> function); static String join(List<String> list, String separator);  }