i have this algorithm that i want to change it with parallel streams so it makes less time for the calculation, but when i did it i got the error Local variable defined in an enclosing scope must be final for realiseValeur, nbValid and invalidite.So how can i work with parelle streams in this algorithm. This is my algorithm in which i want to work with parallelstreams :
@Override
public Map<String, Double> getMapRealise(Date date, String code, Long pc) {
Map<String, Double> map = new HashMap<>();
List<Period> periodList = this.getListPeriod(date, code);
Double realiseValeur = 0.0;
Double invalidite = 0.0;
if (periodList != null) {
for (Period period : periodList) {
Double periode = this.getResolutionTraduiteEnHeures(period.getResolution().getV());
// Date dateDebutPrevisionnel =
// this.getDateDebutPrevisionnel(period.getTimeInterval().getV());
Double nbValid = 0.0;
for (Pt pt : period.getListPt()) {
realiseValeur += periode * pt.getQ().getV() / pcnTranche / NBR_HEURES_PAR_JOURS;
nbValid = nbValid + pt.getCq().getV();
}
if ((nbValid * periode) < NBR_HEURES_MINE_PAR_JOURS) {
invalidite++;
}
}
}
else {
LOGGER.warn( "n existe pas ");
}
map.put(REALISE_VALEUR, realiseValeur);
map.put(REALISE_INVALIDITE, invalidite);
return map;}
I tried this but i got the error Local variable defined in an enclosing scope must be final for realiseValeur, nbValid and invalidite:
@Override
public Map<String, Double> getMapRealise(Date date, String code, Long pc) {
Map<String, Double> map = new HashMap<>();
List<Period> periodList = this.getListPeriod(date, code);
Double realiseValeur = 0.0;
Double invalidite = 0.0;
if (periodList != null) {
periodList.parallelStream().forEach(period -> {
Double periode = this.getResolutionTraduiteEnHeures(period.getResolution().getV());
// Date dateDebutPrevisionnel =
// this.getDateDebutPrevisionnel(period.getTimeInterval().getV());
Double nbValid = 0.0;
period.getListPt().parallelStream().forEach(pt -> {
realiseValeur += periode * pt.getQ().getV() / pcnTranche / NBR_HEURES_PAR_JOURS;
nbValid = nbValid + pt.getCq().getV();
});
if ((nbValid * periode) < NBR_HEURES_MINE_PAR_JOURS) {
invalidite++;
}
});
}
else {
LOGGER.warn("n existe pas ");
}
map.put(REALISE_VALEUR, realiseValeur);
map.put(REALISE_INVALIDITE, invalidite);
return map;
}