For 200 lines it probably doesn't make any difference what solution you choose. But if you want a solution which scales to a very long list the correct approach is to zip
the two lists (or streams) together (this way you avoid looping twice), and use a function to produce the combined result. Guava itself provides a zip() method, but you can also write your own.
Not sure what you mean by 'number of vehicles in the queue', but I am going to assume that you want to know how many were left in the end that have not been allowed in yet.
You can create your own class which maps the vehicles arriving with the agenda for that date.
public class VehiclePlanningData {
private final Vehicle vehicle;
private final Planning planning;
public VehiclePlanningData(Vehicle vehicle, Planning planning) {
this.vehicle = vehicle;
this.planning = planning;
}
public Vehicle getVehicle() {
return vehicle;
}
public Planning getPlanning() {
return planning;
}
}
Once you have this you can easily do a reduce()
operation to carry forward any vehicles left over from the previous slot.
So something like this (using the Guava Streams class):
int queue = Streams.zip(vehicles.stream(), planning.stream(), (v, p) -> new VehiclePlanningData(v, p))
.reduce(0, (queue, nextSlot) ->
queue + (nextSlot.getVehicle().getNbOfVehicleArriving()
- nextSlot.getPlanning().getNbOfallowedVehicles(),
(q1, q2) -> q1 + q2);
UPDATE:
Seems that the queue is per time slot. In that case you might want to have a special class like this, which stores the queue size per slot:
public class TimeslotQueue {
private final Date date;
private final int queueSize;
public VehicleQueue(Date date, int queueSize) {
this.date = date;
this.queueSize = queueSize;
}
public Date getDate() {
return date;
}
public int getQueueSize() {
return queueSize;
}
}
Then get a stream of these classes like this:
List<TimeslotQueue> queues = Streams.zip(vehicles.stream(), planning.stream(),
(v, p) -> new TimeslotQueue(v.getDate(),
v.getNbOfVehicleArriving() - p.getNbOfallowedVehicles()))
.collect(Collectors.toList());
This way you have a list of queued vehicles for each date. As a side note, I would use the Java 8 LocalDateTime
not the old Date
class.