How to transpose List<List>?

This is called transposition. The following snippet does what you need:

import java.util.*;
public class ListTranspose {
    public static void main(String[] args) {
        Object[][] data = {
            { "Title", "Data1", "Data2", "Data3" },
            { "A", 2, 3, 4 },
            { "B", 3, 5, 7 },
        };
        List<List<Object>> table = new ArrayList<List<Object>>();
        for (Object[] row : data) {
            table.add(Arrays.asList(row));
        }
        System.out.println(table); //  [[Title, Data1, Data2, Data3],
                                   //   [A, 2, 3, 4],
                                   //   [B, 3, 5, 7]]"
        table = transpose(table);
        System.out.println(table); //  [[Title, A, B],
                                   //   [Data1, 2, 3],
                                   //   [Data2, 3, 5],
                                   //   [Data3, 4, 7]]
    }
    static <T> List<List<T>> transpose(List<List<T>> table) {
        List<List<T>> ret = new ArrayList<List<T>>();
        final int N = table.get(0).size();
        for (int i = 0; i < N; i++) {
            List<T> col = new ArrayList<T>();
            for (List<T> row : table) {
                col.add(row.get(i));
            }
            ret.add(col);
        }
        return ret;
    }
}

See also

  • Wikipedia/Transpose

Here is my solution.Thanks to @jpaugh's code.I hope this will help you.^_^

public static <T> List<List<T>> transpose(List<List<T>> list) {
   final int N = list.stream().mapToInt(l -> l.size()).max().orElse(-1);
   List<Iterator<T>> iterList = list.stream().map(it->it.iterator()).collect(Collectors.toList());
   return IntStream.range(0, N)
       .mapToObj(n -> iterList.stream()
       .filter(it -> it.hasNext())
       .map(m -> m.next())
       .collect(Collectors.toList()))
   .collect(Collectors.toList());
}

This called a transpose operation. A code sample is here, , but will need significant modification as you have an ArrayList of Arrays (what I infer from your question)