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

1.) Predicate

2.)Function

3.)Consumer

4.)Supplier


1.)Predicate

Interface Predicate<T>

{

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<Player> nameFilter=p->p.getName().startsWith("S");

Predicate p constructed to check the name sratrs with "S"

How to call this? uh!!! that's pretty simple, just invoke p.test(playerObject);

if(nameFilter.test(nameFilter))
              return player;

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 

Predicate<Player> nameFilter=p->p.getName().startsWith("S");
    Predicate<Player> ageFilter=p->p.getAge()>35;
    Predicate<Player> gameFilter=p->p.getGame().startsWith("C");

Applying all the filters together  using 'and' 

if(nameFilter.and(ageFilter.and(gameFilter)).test(player))
              return player;


'or'

if(nameFilter.or(ageFilter.or(gameFilter)).test(player))
               filterList.add(player);

'negate'

if(ageFilter.negate().test(player))
                   filterList.add(player);

 

Predicate Functional interface came up with one Static method 'isEqual', used to check the object is same of not

 Predicate<Player> objectCheck=Predicate.isEqual(playerList.get(0));
        if(objectCheck.test(new Player(45,"Ronaldo","FootBall",BigInteger.valueOf(10000000),"Brazil")))       
         return true;

Once clear with Predicates it's very easy to get the concepts of BiPredicate "which takes two input values instead 😉


2.)Function

 public interface Function<T, R>

R apply(T var1);

compose(T var)

andThen(T var)
identity()


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


Function<String,Integer>strLength=f->f.length();
return strLength.apply("sudeep")


Like predicates 'Function' have default methods, it's mainly used for the function chaining.


1.)andthen 2.)compose





Now will see how 'compose' and 'andThen' will behave differently on the above functions while executing the chaining for the input value 2.


andThen

return math1.andThen(math2).apply(2);-----> output 256

compose

return math1.compose(math2).apply(2);-----> output 8

Function have one static method called Identity which will return the 
input as it is , kind of mirroring.


Once done with Function then you can refer Bi-Function easily.

3.)Consumer

Interface 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 chaining

Consumer<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.) Supplier
Interface Supplier<R>
R get()
Supplier will priovide the required object , can be accessed using get
method, note that supplier doesn't have any agruments in the method.
Authcode generator code sample with supplier
String passkey="";
Supplier<Integer> code=()->(int)(Math.random()*10);
for(int i=0;i<size;i++)
passkey+=code.get();









Comments

Popular posts from this blog

Linux Basics

RabbitMQ