Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transpose List<List>?

I have a following ArrayList,

[Title,Data1,Data2,Data3]
[A,2,3,4]
[B,3,5,7]

And I would like to convert this one like this,

[Title,A,B]
[Data1,2,3]
[Data2,3,5]
[Data3,4,7]

I'm bit confused with the approach. Any hint would be much appreciated.

Thanks.

like image 412
ukanth Avatar asked May 31 '10 07:05

ukanth


People also ask

How do I transpose a list in Python?

Transpose with built-in function zip() You can transpose a two-dimensional list using the built-in function zip() . zip() is a function that returns an iterator that summarizes the multiple iterables ( list , tuple , etc.). In addition, use * that allows you to unpack the list and pass its elements to the function.

How do I transpose a list in R?

Description. Transpose turns a list-of-lists "inside-out"; it turns a pair of lists into a list of pairs, or a list of pairs into pair of lists. For example, if you had a list of length n where each component had values a and b , transpose() would make a list with elements a and b that contained lists of length n.


2 Answers

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
like image 69
polygenelubricants Avatar answered Sep 17 '22 16:09

polygenelubricants


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());
}
like image 20
Nick Micheal Avatar answered Sep 16 '22 16:09

Nick Micheal