JAVA 8 Predefined Functions
Java 8 has considerable up-gradation when compare to other releases, I am going to discuss some key concepts that are very much required for the understanding of functional programming way of implementation.
I have written one Demo problem which covers all the concepts, better refer to this code while reading.
Better to understand what is lamdas, default methods, and static methods, this article will be a quick reference. 👍
4 Key Predefined Functions Interfaces
boolean test(T t);// functional interface
default Predicate<T> add();
default Predicate<T> or();
default Predicate<T> negate()
static <T> Predicate<T> isEqual(Object targetRef) ====>single static method
This predicate is mainly used for the conditions check, which contains a single abstract method, 3 default methods, and single static methods.
Single Abstract Method test(T k);
This method will take a single input and returns a boolean as result.
Predicate p constructed to check the name sratrs with "S"
How to call this? uh!!! that's pretty simple, just invoke p.test(playerObject);
where "nameFilter" is the predicate name;
DefaultMethods -> (and, or, negate)this can give extra operations on predicates , called predicate joining
Below I created 3 predicates
Applying all the filters together using 'and'
Predicate Functional interface came up with one Static method 'isEqual', used to check the object is same of not
Once clear with Predicates it's very easy to get the concepts of BiPredicate "which takes two input values instead 😉
public interface Function<T, R>
R apply(T var1);
Like Predicate Function also take single input but can return to any type R. where apply is the Single Abstract Method for the interface.
To return the length of a String of any String we can use the below
Like predicates 'Function' have default methods, it's mainly used for the function chaining.
Now will see how 'compose' and 'andThen' will behave differently on the above functions while executing the chaining for the input value 2.
return math1.andThen(math2).apply(2);-----> output 256
return math1.compose(math2).apply(2);-----> output 8Function have one static method called Identity which will return theinput as it is , kind of mirroring.Once done with Function then you can refer Bi-Function easily.3.)ConsumerInterface Consumer <T>public void accept(T t);default andThen()Consumer will just accept the object which does not return anything.Consumer<String> consumer = c -> System.out.println(c);consumer.accept(a);
consumer.accept(b);The above code will print values of 'a' and 'b' string contents
andThen() usage is same as the Function just for chainingConsumer<String> consumer = c -> System.out.println(c);
Consumer<String> consumer2= c->System.out.print("Full name is :"+ a+" ");consumer2.andThen(consumer).accept(b);
Bi-Consumer also like same where we can give 2 aruments and operate on then based on the requirement.4.) SupplierInterface Supplier<R>R get()Supplier will priovide the required object , can be accessed using getmethod, note that supplier doesn't have any agruments in the method.Authcode generator code sample with supplierString passkey="";
Post a Comment