¿Cómo convierto mi bucle "for" en un bucle "for of"? [duplicar]

Nov 28 2020

Estoy tratando de convertir esto en bucle for para cada bucle.

        for (int i = 0; i < appointments.size(); i++) {
            if (appointments.get(i).equals(appointment)) { 
              appointments.get(i).setAvailability(true);
            }
        }
   

Respuestas

ElliottFrisch Nov 28 2020 at 18:46

for-each appten appointments. Me gusta,

for (Appointment appt : appointments) {
    if (appt.equals(appointment)) {
        appt.setAvailability(true);
    }
}

O bien, si el uso de Java 8+ usted podría streamel appointments. Me gusta,

appointments.stream()
        .filter(x -> x.equals(appointment))
        .forEach(appt -> appt.setAvailability(true));