Neha:
Tengo tres listas,
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));
Quiero recorrer a través de cada uno a la vez, e imprimir
1 30 30
1.5 25 25
¿Cómo puedo hacer esto usando java-8 API corriente?
Xingbin:
Esto funciona cuando el tamaño de las listas son iguales o diferentes:
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();
});