@Test public void groupテスト() { class Groupee { String g; String n; public Groupee(String g, String n) {this.g = g;this.n = n;} public String getGroup() {return g;} public String getName() {return n;} @Override public boolean equals(Object o) { if (o == null || !(o instanceof Groupee)) return false; Groupee t = (Groupee) o; return this.g.equals(t.getGroup()) && this.n.equals(t.getName()); } } final List<String> groups = Lists.gen("A", "A", "B", "B", "B", "C"); final List<String> names = Lists.gen("a", "b", "c", "d", "e", "f"); List<Groupee> elements = new ArrayList<Groupee>(); for (int i = 0; i < groups.size(); i++) { elements.add(new Groupee(groups.get(i), names.get(i))); } Map<String, List<Groupee>> results = Lists.group(elements, new GroupCall<Groupee>() { public String group(Groupee group, int index, int total) { return group.getGroup(); } }); Map<String, List<Groupee>> expected = new HashMap<String, List<Groupee>>(); expected.put("A", Lists.gen(new Groupee("A", "a"), new Groupee("A", "b"))); expected.put("B", Lists.gen(new Groupee("B", "c"), new Groupee("B", "d"), new Groupee("B", "e"))); expected.put("C", Lists.gen(new Groupee("C", "f"))); Assert.assertEquals(expected, results); }
public static <T> Map<String, List<T>> group(List<T> elements, GroupCall<T> callback) { if (elements == null || callback == null || elements.size() == 0) return Collections.<String, List<T>>emptyMap(); int index = 0; int total = elements.size(); Map<String, List<T>> grouped = new HashMap<String, List<T>>(); for (T t : elements) { String key = callback.group(t, index++, total); if (key != null) { List<T> g = grouped.get(key); if (g != null) { g.add(t); } else { g = Reflects.make(elements.getClass()); g.add(t); grouped.put(key, g); } } } return grouped; }
Lists { public static <T> Map<String, List<T>> group(List<T> elements, GroupCall<T> callback) { if (elements == null || callback == null || elements.size() == 0) return Collections.<String, List<T>>emptyMap(); int index = 0; int total = elements.size(); Map<String, List<T>> grouped = new HashMap<String, List<T>>(); for (T t : elements) { String key = callback.group(t, index++, total); if (key != null) { List<T> g = grouped.get(key); if (g != null) { g.add(t); } else { g = Reflects.make(elements.getClass()); g.add(t); grouped.put(key, g); } } } return grouped; } }
Lists { public static <T> Map<String, List<T>> group(List<T> elements, GroupCall<T> callback) { if (elements == null || callback == null || elements.size() == 0) return Collections.<String, List<T>>emptyMap(); int index = 0; int total = elements.size(); Map<String, List<T>> grouped = new HashMap<String, List<T>>(); for (T t : elements) { String key = callback.group(t, index++, total); if (key != null) { List<T> g = grouped.get(key); if (g != null) { g.add(t); } else { g = Reflects.make(elements.getClass()); g.add(t); grouped.put(key, g); } } } return grouped; } private  Lists(); }
Lists { public static <T> Map<String, List<T>> group(List<T> elements, GroupCall<T> callback) { if (elements == null || callback == null || elements.size() == 0) return Collections.<String, List<T>>emptyMap(); int index = 0; int total = elements.size(); Map<String, List<T>> grouped = new HashMap<String, List<T>>(); for (T t : elements) { String key = callback.group(t, index++, total); if (key != null) { List<T> g = grouped.get(key); if (g != null) { g.add(t); } else { g = Reflects.make(elements.getClass()); g.add(t); grouped.put(key, g); } } } return grouped; } private  Lists(); static List<T> gen(T... elements); static List<T> gen(int size, GenCall<T> generator); static U gen(int size, Class<U> container, GenCall<T> generator); static List<T> each(List<T> elements, EachCall<T> callback); static List<T> collect(List<T> elements, CollectCall<T> callback); static Map<String, List<T>> group(List<T> elements, GroupCall<T> callback); static boolean isEmpty(List<T> suspect); static PartialList<T> emptyPartialList(); }
Lists { public static <T> Map<String, List<T>> group(List<T> elements, GroupCall<T> callback) { if (elements == null || callback == null || elements.size() == 0) return Collections.<String, List<T>>emptyMap(); int index = 0; int total = elements.size(); Map<String, List<T>> grouped = new HashMap<String, List<T>>(); for (T t : elements) { String key = callback.group(t, index++, total); if (key != null) { List<T> g = grouped.get(key); if (g != null) { g.add(t); } else { g = Reflects.make(elements.getClass()); g.add(t); grouped.put(key, g); } } } return grouped; } private  Lists(); static List<T> gen(T... elements); static List<T> gen(int size, GenCall<T> generator); static U gen(int size, Class<U> container, GenCall<T> generator); static List<T> each(List<T> elements, EachCall<T> callback); static List<T> collect(List<T> elements, CollectCall<T> callback); static Map<String, List<T>> group(List<T> elements, GroupCall<T> callback); static boolean isEmpty(List<T> suspect); static PartialList<T> emptyPartialList(); @SuppressWarnings("rawtypes")
    static final PartialList EMPTY_PARTIAL_LIST; }