java 如何转置 List<List>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2941997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to transpose List<List>?
提问by ukanth
I have a following ArrayList,
我有一个以下 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.
谢谢。
采纳答案by polygenelubricants
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
也可以看看
回答by Nick Micheal
Here is my solution.Thanks to @jpaugh's code.I hope this will help you.^_^
这是我的解决方案。感谢@jpaugh 的代码。我希望这对你有帮助。^_^
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());
}
回答by Fergal
The technique is called transposing. Example of an implementation.
该技术称为转置。一个实现的例子。
public static MyObject [][] transpose(MyObject [][] m){
int r = m.length;
int c = m[r].length;
MyObject [][] t = new MyObject[c][r];
for(int i = 0; i < r; ++i){
for(int j = 0; j < c; ++j){
t[j][i] = m[i][j];
}
}
return t;
}
回答by mickthompson
something like this maybe
像这样的事情也许
List<List<String>> list = new ArrayList<List<String>>(firstList.size());
for(int i = 0; i < firstList.size(); i++) {
list.add(Arrays.asList(
firstList.get(i),
secondList.get(i),
thirdList.get(i))
);
}
回答by Andreas Dolk
The mathematics behind: you need to transposethe matrix. It's easier if you use a 2-dimensional array or a 'List of Lists', which is pretty much he same with collections. A List of arrays works too, but it is slightly more confusing.
背后的数学原理:您需要转置矩阵。如果使用二维数组或“列表列表”会更容易,这与集合几乎相同。数组列表也可以,但稍微有点混乱。
This wikipedia articleshows some algorithms for transposition.
这篇维基百科文章展示了一些换位算法。
回答by Midhat
回答by James P.
Do you have a fixed number of ArrayLists and are they of fixed size to begin with? If it is fixed, what you can do is have an int index value and process each ArrayList in turn in the same loop. You can then transfer each value to a temporary ArrayList and then place a reference to this in a final ArrayList for output.
您是否有固定数量的 ArrayList 并且它们的大小是否固定?如果它是固定的,你可以做的是有一个 int 索引值并在同一个循环中依次处理每个 ArrayList。然后,您可以将每个值传输到临时 ArrayList,然后在最终的 ArrayList 中放置对此的引用以供输出。
Sounds confusing? Here's a rough solution:
听起来很混乱?这是一个粗略的解决方案:
ArrayList tempList = new ArrayList();
ArrayList outputList = new ArrayList();
for(index=0;index<list1.getsize();index++){
// Add null checks and other validation here
tempList.add( list1.get(index) );
tempList.add( list2.get(index) );
tempList.add( list3.get(index) );
outputList.add( tempList );
}
回答by Markus Lausberg
Check whether all lists have the same size.
检查所有列表是否具有相同的大小。
Place the informations in a Matrix (List with List elements, for example) to get the number of lists and size of a list.
将信息放在矩阵中(例如,带有列表元素的列表)以获取列表的数量和列表的大小。
Create a new Matrix with the rotated size informations. (3x4 to 4x3)
使用旋转后的大小信息创建一个新矩阵。(3x4 到 4x3)
Implement 2 For loops and place the elements into the new matrix.
实现 2 For 循环并将元素放入新矩阵中。
回答by Peter Tillemans
If it is for a datamigration task, you might consider your friendly spreadsheet if the size is not too big.
如果是用于数据迁移任务,如果大小不是太大,您可以考虑使用友好的电子表格。
For matrix manipulation stuff there is the jScience library which has matrix support. For just transposing a metrix this would be overkill, but it depends what needs to be done with it.
对于矩阵操作,有支持矩阵的 jScience 库。对于仅转置矩阵这将是矫枉过正,但这取决于需要用它做什么。

