Posts

Showing posts from February, 2020

Java Functional Programming : 6. Function

This is the multi part series on Java Functional Programming 6. Function Apart from the apply method (which is abstract ) the Function functional interface also has a static method and two default methods. We have seen the usage of apply method. It converts (or applies) one type to another. Let us investigate the default and static methods of this functional interface . Category Function Interface Definition Function<T, R> Abstract Method R apply (T t) Static Method <T> Function<T,T> identity() Default Method 1 <V> Function<T,V> andThen(Function<? super R,? extends V> after) Default Method 2 <V> Function<V,R> compose(Function<? super V,? extends T> before) Let us see how these methods look from the inside. The static function identity gives back the lambda function which always returns ...

Java Functional Programming : 5. Predicate

This is the multi part series on Java Functional Programming 5. Predicate The Predicate is used for making a Boolean check. Apart from the abstract method test it has 1 static and 3 default methods. Category Predicate Interface Definition Predicate<T> Abstract Method boolean test(T t) Static Method <T> Predicate<T> isEqual(Object targetRef) Default Method 1 Predicate<T> and(Predicate<? super T> other) Default Method 2 Predicate<T> or(Predicate<? super T> other) Default Method 3 Predicate<T> negate( ) Let us first see how the body of each method looks. The three default methods – and , or and negate actually represent the logical checks - AND , OR and NOT . Since these return a Predicate these can be chained too just like the Consumer we saw earlier. It is not a good idea to chain too many operators as they may add to ...