Lets say I have two lists of cars, and I want to stream through them, firstly checking they are the same type, and then and compare them based on some features, and depending on the outcomes I will perform some logic.
for(Car newCar : newCarLot) {
for(Car oldCar : oldCarLot) {
if(oldCar.getType() == newCar.getType()) {
if(oldCar.getTransmission() == newCar.getTransmission()) {
// do something, for example println
}
if(oldCar.getColour() == newCar.getColour()) {
// do something else
}
}
}
}
How would I perform this in streams?
Something like this one?
public class Main {
public static void main(String[] args) {
List<Integer> numbers1 = Arrays.asList(1, 2, 3);
List<Integer> numbers2 = Arrays.asList(3, 4);
numbers1.stream().flatMap(
a -> numbers2.stream().map(b -> new int[]{a, b})
).collect(Collectors.toList()).forEach(c -> System.out.println(Arrays.toString(c)));
}
}
Result:
[1, 3]
[1, 4]
[2, 3]
[2, 4]
[3, 3]
[3, 4]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.