After working and testing around this best option, I came up with:
IntStream.range(0, array.length).forEach(x -> Arrays.setAll(array[x], y -> builder.build2Dobject(x, y)));
(In a specific case, I assumed that it would be:
IntStream.range(0, array.length).forEach(x -> Arrays.setAll(array[x], y -> String.format("%c%c", letter(x), letter(y)));
for a three-dimensional array, simply:
IntStream.range(0, array.length).forEach(x -> IntStream.range(0, array[x].length).forEach(y -> Arrays.setAll(array[x][y], z -> builder.build3Dobject(x, y, z))));
This is the code that allows the program to choose the fastest option:
public static void fill2DArray(Object[][] array, Object2DBuilderReturn builder){ int totalLength = array.length * array[0].length; if (totalLength < 200){ for(int x = 0; x < array.length; x++){ for (int y = 0; y < array[x].length; y++){ array[x][y] = builder.build2Dobject(x, y); } } } else if (totalLength >= 200 && totalLength < 1000){ IntStream.range(0, array.length).forEach(x -> Arrays.setAll(array[x], y -> builder.build2Dobject(x, y))); } else { IntStream.range(0, array.length).forEach(x -> Arrays.setAll(array[x], y -> builder.build2Dobject(x, y))); } }
functional interface:
@FunctionalInterface public interface Object2DBuilderReturn<T> { public T build2Dobject(int a, int b); }
Angelo alvisi
source share