I have this piece of code, to extract from a List of DeviceEvents the ones with some condition
List<DeviceEvent> deviceEvents = new ArrayList<>();
deviceEventService
.findAll(loggedInUser())
.filter(this::isAlarmMessage)
.iterator()
.forEachRemaining(deviceEvents::add);
private boolean isAlarmMessage (DeviceEvent deviceEvent) {
return AlarmLevelEnum.HIGH == deviceEvent.getDeviceMessage().getLevel();
}
but I got this compilation error:
The method filter(this::isAlarmMessage) is undefined for the type
Iterable<DeviceEvent>
findAll returns a Iterable<DeviceEvent> filter method should be called on Stream object.
List<DeviceEvent> deviceEvents = deviceEventService
.findAll(loggedInUser()).stream()
.filter(this::isAlarmMessage)
.collect(toList());
Also you should not create empty ArrayList to collect results. Use Stream.collect with appropriate collector.
If findAll returns Iterable, firstly you need convert it to stream.
StreamSupport.stream(
deviceEventService.findAll(loggedInUser()).spliterator(), false)
.stream() // and so on
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With