Search⌘ K
AI Features

Solution: The Patient Vitals Monitor

Explore how to implement functional programming concepts in Java to monitor patient vitals efficiently. Learn to filter patient data using Streams, handle optional results safely, and generate alerts for critical cases or confirm when all patients are stable.

We'll cover the following...
Java 25
import java.util.List;
import java.util.Optional;
public class VitalsMonitor {
public record PatientRecord(String name, int heartRate, int oxygenLevel) {}
public static String monitorVitals(List<PatientRecord> patients) {
return patients.stream()
// Filter for either high heart rate OR low oxygen
.filter(p -> p.heartRate > 120 || p.oxygenLevel < 90)
// Stop at the very first match we find
.findFirst()
// If present, transform the record into an alert string
.map(p -> "ALERT: " + p.name)
// If the Optional is empty (no matches), return this default
.orElse("All Clear");
}
public static void main(String[] args) {
List<PatientRecord> currentBatch = List.of(
new PatientRecord("Alex", 72, 98),
new PatientRecord("Sam", 130, 95),
new PatientRecord("Jordan", 65, 88)
);
List<PatientRecord> stableBatch = List.of(
new PatientRecord("Taylor", 80, 99),
new PatientRecord("Casey", 75, 96)
);
System.out.println("Batch 1 Status: " + monitorVitals(currentBatch));
System.out.println("Batch 2 Status: " + monitorVitals(stableBatch));
}
}
...