I try, using streams, to get a Map of the form HashMap <Integer, List <Integer >> (while getting a Map of the form HashMap <Integer, List <int [] >>) from Collection <int []>. The code is as follows:
final int[][] OFFSETS = {{1,1}, {2,2}, {3,3}, {4,4}}; Map<Integer, int[]> coordinates = new HashMap<>(); Map<int[], Integer> coordinates_inverse = new HashMap<>(); int[][] tmp = new int[64][2]; int k = 0; for (int i = 1; i <= 8; i++) { for (int j = 1; j <= 8; j++) { int[] member = new int[]{i, j}; tmp[k] = member; coordinates.put(k, tmp[k]); coordinates_inverse.put(tmp[k], k); k++; } } Map<Integer, List<int[]>> test = coordinates.values().stream() .collect(Collectors.toMap(c -> coordinates_inverse.get(c), c -> Arrays.stream(OFFSETS) .map(i -> i = new int[]{i[0] + c[0], i[1] + c[1]}) .filter(i -> i[0] > 0 && i[0] < 9 && i[1] > 0 && i[1] < 9) .sorted((i, j) -> { if (i[0] == j[0]) return i[1] - j[1]; else return i[0] - j[0]; }) // .map(i-> Arrays.stream(i).map(coordinates_inverse::get).boxed()) .collect(Collectors.toList()))); for (Map.Entry<Integer, List<int[]>> e: test.entrySet()){ System.out.printf("Изначальная позиция " + e.getKey() + "\n" + "Ходы " + Arrays.deepToString(e.getValue().toArray()) + "\n"); } This code when compiling produces a keymap with values, all elements of which are null, although they must contain types Integer.
Are there alternative ways to fill with Map values or is it possible to perform all transformations in one stream?