Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of List of Lists (Flatten a ArrayList with N-Depth)

I am trying to Flatten a ArrayList with N-Depth. For this I tried using flapMap method of Stream API. I am able to get it. But I have to use flatMap() method repeatedly as per the number of list of lists. If I am using one more flatMap() method, it shows compile time error. Is there any way to dynamically get it done.

This is the code that I used:

List<Integer> list1 = Arrays.asList(4,5,6);
List<Integer> list2 = Arrays.asList(7,8,9);

List<List<Integer>> listOfLists = Arrays.asList(list1, list2);

List<List<List<Integer>>> listA = Arrays.asList(listOfLists);
List<List<List<List<Integer>>>> listB = Arrays.asList(listA);

List<Integer> listFinal = listB.stream()
    .flatMap(x -> x.stream())
    .flatMap(x -> x.stream())
    .flatMap(x -> x.stream())
    .collect(Collectors.toList());
//In the above line, If I use listA instead of listB, it is showing error.

listFinal.forEach(x-> System.out.println(x));
like image 203
Klaus Avatar asked Dec 01 '25 22:12

Klaus


1 Answers

This seems to work for me, using recursion. When given a list

  • check if it is a list of Integer, in which case return it.
  • otherwise it is a list of lists. FlatMap it (to remove 1 level of nesting), and flatten the resulting list.
private static List<Integer> flatten(List<?> list) {
    if (list.get(0) instanceof Integer) {
        return (List<Integer>) list;
    }

    List<List<?>> listOfLists = (List<List<?>>) list;
    return flatten(listOfLists.stream()
            .flatMap(Collection::stream)
            .collect(Collectors.toList()));
}

then

public static void main(String[] args) {
    List<List<List<List<List<Integer>>>>> listC = Arrays.asList(
            Arrays.asList(
                    Arrays.asList(
                            Arrays.asList(
                                    Arrays.asList(0, 1),
                                    Arrays.asList(2, 3, 4)
                            ),
                            Arrays.asList(
                                    Arrays.asList(5),
                                    Arrays.asList(6, 7),
                                    Arrays.asList(8, 9)
                            )
                    ),
                    Arrays.asList(
                            Arrays.asList(
                                    Arrays.asList(10, 11),
                                    Arrays.asList(12, 13, 14)
                            ),
                            Arrays.asList(
                                    Arrays.asList(15),
                                    Arrays.asList(16, 17),
                                    Arrays.asList(18, 19)
                            )
                    )
            ),
            Arrays.asList(
                    Arrays.asList(
                            Arrays.asList(
                                    Arrays.asList(20, 21),
                                    Arrays.asList(22, 23, 24)
                            ),
                            Arrays.asList(
                                    Arrays.asList(25),
                                    Arrays.asList(26, 27),
                                    Arrays.asList(28, 29)
                            )
                    ),
                    Arrays.asList(
                            Arrays.asList(
                                    Arrays.asList(30, 31),
                                    Arrays.asList(32, 33, 34)
                            ),
                            Arrays.asList(
                                    Arrays.asList(35),
                                    Arrays.asList(36, 37),
                                    Arrays.asList(38, 39)
                            )
                    )
            )
    );

    List<Integer> result = flatten(listC);

    System.out.println(listC);
    System.out.println(result);

}

prints

[[[[[0, 1], [2, 3, 4]], [[5], [6, 7], [8, 9]]], [[[10, 11], [12, 13, 14]], [[15], [16, 17], [18, 19]]]], [[[[20, 21], [22, 23, 24]], [[25], [26, 27], [28, 29]]], [[[30, 31], [32, 33, 34]], [[35], [36, 37], [38, 39]]]]]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]

There is a bit of Unchecked casting going on, but not sure how to do without

like image 65
Bentaye Avatar answered Dec 04 '25 11:12

Bentaye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!