@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 String[] groups = {"A", "A", "B", "B", "B", "C"}; final String[] names = {"a", "b", "c", "d", "e", "f"}; final Groupee[] elements = new Groupee[groups.length]; for (int i = 0; i < groups.length; i++) { elements[i] = new Groupee(groups[i], names[i]); } Map<String, Groupee[]> results = Arrays.group(elements, new GroupCall<Groupee>() { public String group(Groupee group, int index, int total) { return group.getGroup(); } }); Groupee[] groupA = results.get("A"); Groupee[] groupB = results.get("B"); Groupee[] groupC = results.get("C"); Assert.assertArrayEquals(Arrays.gen(elements[0], elements[1]), groupA); Assert.assertEquals(2, groupA.length); Assert.assertArrayEquals(Arrays.gen(elements[2], elements[3], elements[4]), groupB); Assert.assertEquals(3, groupB.length); Assert.assertArrayEquals(Arrays.gen(elements[5]), groupC); Assert.assertEquals(1, groupC.length); }
@SuppressWarnings("unchecked") public static <T> Map<String, T[]> group(T[] elements, GroupCall<T> callback) { if (elements == null || callback == null || elements.length == 0) return Collections.<String, T[]>emptyMap(); int index = 0; int total = elements.length; 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 = new ArrayList<T>(); g.add(t); grouped.put(key, g); } } } Map<String, T[]> results = new HashMap<String, T[]>(grouped.size()); T[] zero = (T[]) Array.newInstance(elements.getClass().getComponentType(), 0); for (String key : grouped.keySet()) { List<T> g = grouped.get(key); results.put(key, g.toArray(zero)); } return results; }
Arrays { @SuppressWarnings("unchecked") public static <T> Map<String, T[]> group(T[] elements, GroupCall<T> callback) { if (elements == null || callback == null || elements.length == 0) return Collections.<String, T[]>emptyMap(); int index = 0; int total = elements.length; 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 = new ArrayList<T>(); g.add(t); grouped.put(key, g); } } } Map<String, T[]> results = new HashMap<String, T[]>(grouped.size()); T[] zero = (T[]) Array.newInstance(elements.getClass().getComponentType(), 0); for (String key : grouped.keySet()) { List<T> g = grouped.get(key); results.put(key, g.toArray(zero)); } return results; } }
Arrays { @SuppressWarnings("unchecked") public static <T> Map<String, T[]> group(T[] elements, GroupCall<T> callback) { if (elements == null || callback == null || elements.length == 0) return Collections.<String, T[]>emptyMap(); int index = 0; int total = elements.length; 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 = new ArrayList<T>(); g.add(t); grouped.put(key, g); } } } Map<String, T[]> results = new HashMap<String, T[]>(grouped.size()); T[] zero = (T[]) Array.newInstance(elements.getClass().getComponentType(), 0); for (String key : grouped.keySet()) { List<T> g = grouped.get(key); results.put(key, g.toArray(zero)); } return results; } private  Arrays(); }
Arrays { @SuppressWarnings("unchecked") public static <T> Map<String, T[]> group(T[] elements, GroupCall<T> callback) { if (elements == null || callback == null || elements.length == 0) return Collections.<String, T[]>emptyMap(); int index = 0; int total = elements.length; 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 = new ArrayList<T>(); g.add(t); grouped.put(key, g); } } } Map<String, T[]> results = new HashMap<String, T[]>(grouped.size()); T[] zero = (T[]) Array.newInstance(elements.getClass().getComponentType(), 0); for (String key : grouped.keySet()) { List<T> g = grouped.get(key); results.put(key, g.toArray(zero)); } return results; } private  Arrays(); static T[] gen(T... elements); @SuppressWarnings("unchecked") static T[] gen(int size, GenCall<T> generator); static int contain(T[] t, T suspect); static Iterator<T> iterator(T... elements); @SuppressWarnings("unchecked") static T[] merge(final T[] former, final T... latter); static T[] each(T[] elements, EachCall<T> callback); @SuppressWarnings("unchecked") static T[] collect(T[] elements, CollectCall<T> callback); @SuppressWarnings("unchecked") static Map<String, T[]> group(T[] elements, GroupCall<T> callback); static boolean isEmpty(T[] suspect); static boolean isNotEmpty(T[] suspect); static T[] slice(T[] slicee, int start); @SuppressWarnings("unchecked") static T[] slice(T[] slicee, int start, int length); }
Arrays { @SuppressWarnings("unchecked") public static <T> Map<String, T[]> group(T[] elements, GroupCall<T> callback) { if (elements == null || callback == null || elements.length == 0) return Collections.<String, T[]>emptyMap(); int index = 0; int total = elements.length; 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 = new ArrayList<T>(); g.add(t); grouped.put(key, g); } } } Map<String, T[]> results = new HashMap<String, T[]>(grouped.size()); T[] zero = (T[]) Array.newInstance(elements.getClass().getComponentType(), 0); for (String key : grouped.keySet()) { List<T> g = grouped.get(key); results.put(key, g.toArray(zero)); } return results; } private  Arrays(); static T[] gen(T... elements); @SuppressWarnings("unchecked") static T[] gen(int size, GenCall<T> generator); static int contain(T[] t, T suspect); static Iterator<T> iterator(T... elements); @SuppressWarnings("unchecked") static T[] merge(final T[] former, final T... latter); static T[] each(T[] elements, EachCall<T> callback); @SuppressWarnings("unchecked") static T[] collect(T[] elements, CollectCall<T> callback); @SuppressWarnings("unchecked") static Map<String, T[]> group(T[] elements, GroupCall<T> callback); static boolean isEmpty(T[] suspect); static boolean isNotEmpty(T[] suspect); static T[] slice(T[] slicee, int start); @SuppressWarnings("unchecked") static T[] slice(T[] slicee, int start, int length);  }