[英]How to traverse multiple list using Java 8 stream?
I have three lists, 我有三个清单,
List<Double> list1= new ArrayList(List.of(1, 1.5));
List<Double> list2= new ArrayList(List.of(30, 25));
List<Double> list3= new ArrayList(List.of(30, 25));
I want to traverse through each simultaneously, and print 我想同时遍历每个,并打印
1 30 30
1.5 25 25
How can I do this using java-8 stream API? 我怎么能用java-8流API做到这一点?
You could do (assuming all lists have the same size): 你可以做(假设所有列表都有相同的大小):
IntStream.range(0, list1.size())
.forEach(x -> Syso(list1.get(x) + " " + list2.get(x) + " " + list3.get(x)))
But there is no real benefit here, just use a plain for loop. 但是这里没有真正的好处,只需使用普通的for循环。
And btw don't wrap these with new ArrayList(List.of(30, 25));
并且btw不会用
new ArrayList(List.of(30, 25));
包装它们new ArrayList(List.of(30, 25));
, this would be enough: ,这就足够了:
List<Double> list1 = List.of(1D, 1.5D)
List.of(..)
is a static factory returning a List
itself, there is no need to wrap it into a ArrayList::new
constructor. List.of(..)
是一个返回List
本身的静态工厂,不需要将它包装到ArrayList::new
构造函数中。
Here is the code you probably need, however, don't underestimate the power for a for-loop
: 以下是您可能需要的代码,但是,不要低估
for-loop
:
// Here you need to get the max of all the List sizes to traverse all Lists
int maxSize = Math.max(list1.size(), Math.max(list2.size(), list3.size()));
IntStream.range(0, maxSize) // Iterate 0 .. minSize-1
.mapToObj(i -> Stream.of(list1, list2, list3) // Map each index
.filter(list -> i < list.size()) // Secure the existing index
.map(list -> list.get(i)) // Get the item
.collect(Collectors.toList())) // Collect to List
.forEach(System.out::println); // And iterate (print)
Output: 输出:
[1, 30, 30]
[1.5, 25, 25]
In case of variable list size, the traversal iteration is safe because I touch only the existing indices. 在变量列表大小的情况下,遍历迭代是安全的,因为我只触摸现有索引。
No need for streams - just transpose the lists: 不需要流 - 只需转置列表:
private static class Transpose<T> extends AbstractList<List<T>> implements List<List<T>> {
private final List<T> [] lists;
public Transpose(List<T>... lists) {
this.lists = lists;
}
private class Column extends AbstractList<T> implements List<T> {
private final int column;
public Column(int column) {
this.column = column;
}
@Override
public T get(int row) {
return lists[row].get(column);
}
@Override
public int size() {
return lists.length;
}
}
@Override
public List<T> get(int column) {
return new Column(column);
}
@Override
public int size() {
return lists[0].size();
}
}
private void test() {
List<Double> list1= Arrays.asList(1.0, 1.5);
List<Double> list2= Arrays.asList(30.0, 25.0);
List<Double> list3= Arrays.asList(30.0, 25.0);
List<List<Double>> matrix = new Transpose(list1, list2, list3);
System.out.println(matrix);
}
Prints: 打印:
[[1.0, 30.0, 30.0], [1.5, 25.0, 25.0]]
[[1.0,30.0,30.0],[1.5,25.0,25.0]]
Obviously this would be more difficult if the lists aren't all the same length. 显然,如果列表长度不同,这将更加困难。
This works when the lists' size are same or different: 这在列表的大小相同或不同时有效:
List<Double> list1 = List.of(1D, 1.5D);
List<Double> list2 = List.of(30D, 25D);
List<Double> list3 = List.of(30D, 25D);
Stream<List<Double>> listStream = Stream.of(list1, list2, list3);
int maxSize = listStream.mapToInt(List::size).max().orElse(0);
IntStream.range(0, maxSize)
.forEach(index -> {
listStream
.filter(list -> list.size() > index)
.forEach(list -> System.out.print(list.get(index) + " "));
System.out.println();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.