Expressions are so confusing
Java 8 release has included many functional interfaces in package java.util.function.These interfaces are heavily used all over the places. Optional and collection Stream API to name a few.
When I started using lambda expressions, I tried to remember the usage and pattren of expression expected in specific method. For example, stream().forEach expects expression with one parameter and void expression while Optional.filter method exptects single parameter and boolean expression. So I realized that I need to understand the usage instead of remembering the pattern.
Understanding common functional interfaces
Function interface:
Function interface has following method
R apply(T t);From the method declaration we can guess that it expects one type of object and returns different type of object. As the name suggests, this interface represents a function which accepts an argument and produces result.
Lets observe some usages of this interface.
Stream -> <R> map(Function<? super T, ? extends R> mapper)From the these details we can say that when we call map method, we need to write a expression, which will get a single parameter and it should return an object. The type of parameter and returned object could be different.
Consumer Interface:
Consumer interface has following method.
void accept(T t);Like any other kind of Consumer, this method cosumes a object and doesnt return anything.
Lets observe some usages of this interface.
Optional -> void ifPresent(Consumer<? super T> consumer)This method take a parameter and its expected that expression should any necessory action based on input.
ifPresent method checks if value is present in optional. If value is not present then it doesn't do anything being a void method. If value is present then executes the expression, passed to the method.
Stream -> void forEach(Consumer<? super T> action);In this example, the stream object executes the provided expression, for the each object in the stream.
Supplier Interface:
Suplier provides or suplies the information/object.It has very simple method.
T get();This method expects no paramer and should have a expression which returns a object.
Lets observe its usage.
Optional -> T orElseGet(Supplier<? extends T> other)orElseGet method returns the value if present or calls the expression provided which in tern, returns a object.
Predicate interface:
Predicate is one of the most widely used funcational Interface.The dictionary meaning of predicate is as follows.
something which is affirmed or denied concerning an argument of a proposition.So basically its a test which returns true or false.
Predicate Interface has following method.
boolean test(T t);From method signature, we can say, it expects a parameter and a expression which is boolean in nature.
Lets observe some examples.
Stream -> <T> filter(Predicate<? super T> predicate);Filter method is used to filter the objects provided in the collection, based on the test provided in expression.
My aim is not to explain about these functional interface, but provide you a vision so that you should be able to deal with any new functional Interface you come across.
I hope this will help you writting your next lamda expression.
No comments:
Post a Comment