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?
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.
And btw don't wrap these with 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.
Here is the code you probably need, however, don't underestimate the power for a 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]]
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();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With