@Test public void testGroupByDerived() { Function<String, String> deriver = s -> s.substring(0, 1); BiPredicate<String, String> comparator = (a, b) -> a.equals(b); List<String> values; List<Pair<String, List<String>>> result; values = asList(); result = BatchUtils.groupByDerived(values, deriver, comparator); assertEquals("[]", result.toString()); values = asList("a1"); result = BatchUtils.groupByDerived(values, deriver, comparator); assertEquals("[(a,[a1])]", result.toString()); values = asList("a1", "a2", "a3"); result = BatchUtils.groupByDerived(values, deriver, comparator); assertEquals("[(a,[a1, a2, a3])]", result.toString()); values = asList("a1", "a2", "b3"); result = BatchUtils.groupByDerived(values, deriver, comparator); assertEquals("[(a,[a1, a2]), (b,[b3])]", result.toString()); values = asList("a1", "b2", "a3", "b4"); result = BatchUtils.groupByDerived(values, deriver, comparator); assertEquals("[(a,[a1]), (b,[b2]), (a,[a3]), (b,[b4])]", result.toString()); values = asList("a1", "a2", "b3", "c4", "a5", "a6", "a7", "c8"); result = BatchUtils.groupByDerived(values, deriver, comparator); assertEquals("[(a,[a1, a2]), (b,[b3]), (c,[c4]), (a,[a5, a6, a7]), (c,[c8])]", result.toString()); }
public static <T, U> List<Pair<U, List<T>>> groupByDerived(List<T> values, Function<T, U> deriver, BiPredicate<U, U> comparator) { List<Pair<U, List<T>>> result = new ArrayList<>(); U previousDerived = null; List<T> batch = null; for (T value : values) { U derived = deriver.apply(value); if (batch == null || !comparator.test(derived, previousDerived)) { batch = new ArrayList<>(); result.add(Pair.of(derived, batch)); } batch.add(value); previousDerived = derived; } return result; }
BatchUtils { public static <T, U> List<Pair<U, List<T>>> groupByDerived(List<T> values, Function<T, U> deriver, BiPredicate<U, U> comparator) { List<Pair<U, List<T>>> result = new ArrayList<>(); U previousDerived = null; List<T> batch = null; for (T value : values) { U derived = deriver.apply(value); if (batch == null || !comparator.test(derived, previousDerived)) { batch = new ArrayList<>(); result.add(Pair.of(derived, batch)); } batch.add(value); previousDerived = derived; } return result; } }
BatchUtils { public static <T, U> List<Pair<U, List<T>>> groupByDerived(List<T> values, Function<T, U> deriver, BiPredicate<U, U> comparator) { List<Pair<U, List<T>>> result = new ArrayList<>(); U previousDerived = null; List<T> batch = null; for (T value : values) { U derived = deriver.apply(value); if (batch == null || !comparator.test(derived, previousDerived)) { batch = new ArrayList<>(); result.add(Pair.of(derived, batch)); } batch.add(value); previousDerived = derived; } return result; } private  BatchUtils(); }
BatchUtils { public static <T, U> List<Pair<U, List<T>>> groupByDerived(List<T> values, Function<T, U> deriver, BiPredicate<U, U> comparator) { List<Pair<U, List<T>>> result = new ArrayList<>(); U previousDerived = null; List<T> batch = null; for (T value : values) { U derived = deriver.apply(value); if (batch == null || !comparator.test(derived, previousDerived)) { batch = new ArrayList<>(); result.add(Pair.of(derived, batch)); } batch.add(value); previousDerived = derived; } return result; } private  BatchUtils(); static List<Pair<U, List<T>>> groupByDerived(List<T> values, Function<T, U> deriver,
            BiPredicate<U, U> comparator); }
BatchUtils { public static <T, U> List<Pair<U, List<T>>> groupByDerived(List<T> values, Function<T, U> deriver, BiPredicate<U, U> comparator) { List<Pair<U, List<T>>> result = new ArrayList<>(); U previousDerived = null; List<T> batch = null; for (T value : values) { U derived = deriver.apply(value); if (batch == null || !comparator.test(derived, previousDerived)) { batch = new ArrayList<>(); result.add(Pair.of(derived, batch)); } batch.add(value); previousDerived = derived; } return result; } private  BatchUtils(); static List<Pair<U, List<T>>> groupByDerived(List<T> values, Function<T, U> deriver,
            BiPredicate<U, U> comparator);  }