Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 streams maps with parameters

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;

    }
like image 579
La Carbonell Avatar asked Jun 30 '17 13:06

La Carbonell


People also ask

Can we use map stream in Java 8?

Java 8 Stream's map method is intermediate operation and consumes single element forom input Stream and produces single element to output Stream. It simply used to convert Stream of one type to another. Let's see method signature of Stream's map method.

Can we use stream with map in Java?

Converting only the Value of the Map<Key, Value> into Stream: This can be done with the help of Map. values() method which returns a Set view of the values contained in this map. In Java 8, this returned set can be easily converted into a Stream of key-value pairs using Set. stream() method.

Can we use filter and map together in Java 8?

Once we have the Stream of Integer, we can apply maths to find out even numbers. We passed that condition to the filter method. If we needed to filter on String, like select all string which has length > 2 , then we would have called filter before map. That's all about how to use map and filter in Java 8.

What is stream () map in Java?

Stream map() in Java with examples Stream map(Function mapper) returns a stream consisting of the results of applying the given function to the elements of this stream. Stream map(Function mapper) is an intermediate operation. These operations are always lazy.


1 Answers

Use a lambda instead of the method reference.

// ...
.map(n -> sendSMS(n, deviceEvent))
// ...
like image 158
Andy Turner Avatar answered Oct 20 '22 00:10

Andy Turner