@Test public void collectテスト() { final String[] elements = {"a", "b", "c", "d", "e"}; final int[] datum = {0, elements.length}; String[] results = Arrays.collect(elements, new CollectCall<String>() { public String collect(String element, int index, int total) { Assert.assertEquals(elements[index], element); Assert.assertEquals(datum[0]++, index); Assert.assertEquals(datum[1], total); return index % 2 == 0 ? element : null; } }); Assert.assertArrayEquals(new String[] {"a", "c", "e"}, results); Assert.assertArrayEquals(new String[] {"a", "b", "c", "d", "e"}, elements); }
@SuppressWarnings("unchecked") public static <T> T[] collect(T[] elements, CollectCall<T> callback) { if (elements == null || callback == null || elements.length == 0) return elements; int index = 0; int total = elements.length; List<T> results = new ArrayList<T>(total); for (T t : elements) { T result = callback.collect(t, index++, total); if (result != null) results.add(result); } T[] zero = (T[]) Array.newInstance(elements.getClass().getComponentType(), 0); return (T[]) results.toArray(zero); }
Arrays { @SuppressWarnings("unchecked") public static <T> T[] collect(T[] elements, CollectCall<T> callback) { if (elements == null || callback == null || elements.length == 0) return elements; int index = 0; int total = elements.length; List<T> results = new ArrayList<T>(total); for (T t : elements) { T result = callback.collect(t, index++, total); if (result != null) results.add(result); } T[] zero = (T[]) Array.newInstance(elements.getClass().getComponentType(), 0); return (T[]) results.toArray(zero); } }
Arrays { @SuppressWarnings("unchecked") public static <T> T[] collect(T[] elements, CollectCall<T> callback) { if (elements == null || callback == null || elements.length == 0) return elements; int index = 0; int total = elements.length; List<T> results = new ArrayList<T>(total); for (T t : elements) { T result = callback.collect(t, index++, total); if (result != null) results.add(result); } T[] zero = (T[]) Array.newInstance(elements.getClass().getComponentType(), 0); return (T[]) results.toArray(zero); } private  Arrays(); }
Arrays { @SuppressWarnings("unchecked") public static <T> T[] collect(T[] elements, CollectCall<T> callback) { if (elements == null || callback == null || elements.length == 0) return elements; int index = 0; int total = elements.length; List<T> results = new ArrayList<T>(total); for (T t : elements) { T result = callback.collect(t, index++, total); if (result != null) results.add(result); } T[] zero = (T[]) Array.newInstance(elements.getClass().getComponentType(), 0); return (T[]) results.toArray(zero); } 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> T[] collect(T[] elements, CollectCall<T> callback) { if (elements == null || callback == null || elements.length == 0) return elements; int index = 0; int total = elements.length; List<T> results = new ArrayList<T>(total); for (T t : elements) { T result = callback.collect(t, index++, total); if (result != null) results.add(result); } T[] zero = (T[]) Array.newInstance(elements.getClass().getComponentType(), 0); return (T[]) results.toArray(zero); } 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);  }