Java 8 streams maps with parameters
Use a lambda instead of the method reference.
// ...
.map(n -> sendSMS(n, deviceEvent))
// ...
... I would like to know if it is possible to pass the parameter
deviceEvent.hasAlarm()
tothis::sendSMS
No, is not possible. When using method reference you can pass only one argument (docs).
But from the code you provided there is no need for such thing. Why to check deviceEvent
for every notification when it is not changing?
Better way:
if(deviceEvent.hasAlarm()) {
notificationsWithGuardians.stream().filter( ...
}
Anyway, if you really want, this can be a solution:
notificationsWithGuardians.stream()
.filter (notification -> notification.getLevels().contains(deviceEvent.getDeviceMessage().getLevel()))
.map(notification -> Pair.of(notification, deviceEvent))
.peek(this::sendSMS)
.forEach(this::sendEmail);
private void sendSMS(Pair<DeviceAlarmNotification, DeviceEvent> pair) { ... }