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?

  • 3
    Personally, I do not understand what result you want? final int [] [] OFFSETS = {{1,1}, {2,2}, {3,3}, {4,4}}; - this is what you have at the entrance. The output should be Map, as I understand it. Which one? What values ​​should there be? - Dmitry
  • Corrected code (commented out one line). As a result, I wanted to get a Map of the form Map <Integer, List <Integer >> in one stream, but the compiler swears at the type. From the current stream, I get a Map of the form Map <Integer, List <int [] >>, in which I need to convert all values ​​of the type List <int []> to List <Integer> using coordinates_inverse elementwise. Then the question arises - is it possible to do everything in one stream, or do you need to then run a separate stream or use other libraries (such as guava) to convert Map values? - Nikolas
  • A stream is called on a specific object. For example, a collection, an array, etc. Accordingly, all the necessary operations on this object can be done in one stream. If you need operations on another object, then you need a second stream. The question requires clarification, since non-working and non-compiled code cannot describe the final task. There is no desire to guess this. Therefore, we come back - what result should be as a result of processing the specified array? - Dmitriy

1 answer 1

After some manipulations it turned out to get the desired Map. If anyone is interested, I quote the code (the error remains unclear for me - the problem was solved when using the TreeMap structure and setting the new Comparator, since the get function for the HashMap coordinates_inverse structure was not worked out):

  final int[][] OFFSETS = {{1,1}, {2,2}, {3,3}, {4,4}}; Map<Integer, int[]> coordinates = new TreeMap<>(); Map<int[], Integer> coordinatesInverse = new TreeMap<>((i, j) -> { if (i[0] == j[0]) return i[1] - j[1]; else return i[0] - j[0]; }); 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]); coordinatesInverse.put(tmp[k], k); k++; } } Map<Integer, List<Integer>> test = coordinates.values().stream() .collect(Collectors.toMap(coordinatesInverse::get, 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) .map(coordinatesInverse::get) .collect(Collectors.toList()))); for (Map.Entry<Integer, List<Integer>> e: test.entrySet()){ System.out.printf("Изначальная позиция " + e.getKey() + "\n" + "Ходы " + Arrays.deepToString(e.getValue().toArray()) + "\n"); } 

Thanks to everyone who responded!