I'm currently learning java 8 streams. I have a list of integers, with values from 1 to 1000. Now, I want to create a new list of integers, where each element is the result of multiplying each element of the numbers list with each other element of the numbers list.
The following code does the job:
List<Integer> numbers = IntStream
.range(1,999)
.mapToObj(Integer::valueOf)
.collect(Collectors.toList());
List<Integer> products = new ArrayList<>();
for (Integer i : numbers) {
for (Integer j : numbers) {
products.add(i*j);
}
}
I would like to know if there's a way to avoid the nested for loop by using streams?