Home > database >  how can I map 2d list and a single list into a 3d list or 3d array in java?
how can I map 2d list and a single list into a 3d list or 3d array in java?

Time:02-27

I have List<List> as [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2]] and List as [1,2,3] how can I map it to a 3d array such that I get output as:

{{ {1,1}, {2,2}, {3,3} },{{1,1}, {2,3}, {3,2}},{{1,2}, {2,1}, {3,3}}, and so on...};
for (int i = 0; i < allpermutations.size(); i  ) {
        for (int j = 0; j < allpermutations.get(i).size(); j  ) {

            System.out.print(allpermutations.get(i).get(j) " ");   //LineX
        }
        System.out.println();
    } */

CodePudding user response:

For each sublist of allpermutations you need to pair it with the 3 values of the other list

List<List<Integer>> allpermutations = Arrays.asList(
        List.of(1, 2, 3), List.of(1, 3, 2), List.of(2, 1, 3),
        List.of(2, 3, 1), List.of(3, 2, 1), List.of(3, 1, 2));
List<Integer> values = List.of(1, 2, 3);

List<List<List<Integer>>> result = new ArrayList<>();
for (List<Integer> permutation : allpermutations) {
    List<List<Integer>> subResult = new ArrayList<>();
    for (int i = 0; i < permutation.size(); i  ) {
        subResult.add(List.of(permutation.get(i), values.get(i)));
    }
    result.add(subResult);
}

Or with Stream

List<List<List<Integer>>> result = allpermutations.stream().map(
        permutation -> IntStream.range(0, permutation.size())
                .mapToObj(i -> List.of(permutation.get(i), values.get(i)))
                .collect(Collectors.toList())
).collect(Collectors.toList());

CodePudding user response:

Let the two lists

List<List<Integer>> xss = Arrays.asList(
        List.of(1, 2, 3), List.of(1, 3, 2), List.of(2, 1, 3),
        List.of(2, 3, 1), List.of(3, 2, 1), List.of(3, 1, 2));

List<Integer> ys = List.of(1, 2, 3);

then using streams your Zip operation could be defined as

Stream<Stream<Stream<Pair<Integer, Integer>>>> zss =    // the list of lists of lists of pairs is...
        ys.stream().map(y ->                            // the main list...
                xss.stream().map(xs ->                  // and for each other list...
                        xs.stream().map(x ->            // join every element...
                                new Pair<>(y, x))));    // with the element of the main list.

or, if you need lists

List<List<List<Pair<Integer, Integer>>>> rs =
        zss.map(zs -> zs.map(z -> z.collect(toList())).collect(toList())).collect(toList());

with output

[[[{1, 1}, {1, 2}, {1, 3}], [{1, 1}, {1, 3}, {1, 2}], [{1, 2}, {1, 1}, {1, 3}], ...

If you want an iterative algorithm, you could write

System.out.print("{");
boolean firstY = true;
for(Integer y: ys) {
    if(firstY)
        firstY = false;
    else
        System.out.print(", ");
    System.out.print("{");
    boolean firstXS = true;
    for(List<Integer> xs: xss) {
        if(firstXS)
            firstXS = false;
        else
            System.out.print(", ");
        System.out.print("{");
        boolean firstX = true;
        for(Integer x: xs) {
            if(firstX)
                firstX = false;
            else
                System.out.print(", ");
            System.out.print("{"   y   ", "   x   "}");
        }
        System.out.print("}");
    }
    System.out.print("}");
}
System.out.print("}");

with the same output

{{{{1, 1}, {1, 2}, {1, 3}}, {{1, 1}, {1, 3}, {1, 2}}, {{1, 2}, {1, 1}, {1, 3}}, ...

Aside (the Pair class)

The tuple is a common type in FP, it could be defined as

static class Pair<A, B> {
    A a;
    B b;
    Pair(A a, B b) { this.a = a; this.b = b; }
    @Override
    public String toString() { return "{"   a   ", "   b   "}"; }
}
  •  Tags:  
  • java
  • Related