I have this couple of functions and I would like to know if it is possible to pass the parameter deviceEvent.hasAlarm()
to .map(this::sendSMS)
private void processAlarm (DeviceEvent deviceEvent) {
notificationsWithGuardians.stream()
.filter (notification -> notification.getLevels().contains(deviceEvent.getDeviceMessage().getLevel()))
.map(this::sendSMS)
.map(this::sendEmail);
}
private DeviceAlarmNotification sendSMS (DeviceAlarmNotification notification, DeviceEvent deviceEvent) {
if (deviceEvent.hasAlarm()) {
}
return notification;
}
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) { ... }
如果引用方法在同一类中,如何创建一个类或成员变量并为其赋值并在提供的引用方法中重用
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.