Search⌘ K
AI Features

Solution: The Patient Vitals Monitor

Explore how to implement a patient vitals monitoring solution using Java functional programming. Learn to process patient lists with streams, apply compound filters for critical conditions, use Optional to handle missing data safely, and produce clear alerts or statuses.

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));
}
}
...