Pages

Saturday 21 September 2019

Java 8 in built functions

IN BUILT FUNCTIONS
Consumer,Supplier,Function and Predicate all are available under java.util.function package.

Comparison  of in built functions
Properties Consumer Supplier Function Predicate
Purpose To consume some input and perform required operation.It won’t return anything To supply some value base on requirement To take some input and perform the required operation and return the result. To some input and perform some conditional checks
Interface Declaration interface Consumer<T>{} interface Supplier<R>{} interface Function<T,R>{} interface Predicate<T>{}
Single abstract method public void accept(T t); public R get(); public R apply(T t); public boolean test(T t);
Default method andThen() NA andThen(),
compose()
and(),or(),
negate()
Static method



identify() isEqual()

How to call in built functions? 
  1. import java.util.function.Consumer;
  2. import java.util.function.Function;
  3. import java.util.function.Predicate;
  4. import java.util.function.Supplier;
  5. public class Test {
  6.     public static void main(String[] args) {
  7.      //void accept(T t);
  8.     Consumer<String> consumer1 = (String s)->{System.out.println(s);};
  9.     Consumer<String> consumer2=(s)->{System.out.println(s);};
  10.     Consumer<String> consumer3=s->System.out.println(s);
  11.       consumer3.accept("Amit");

  12.     //  T get();
  13.       Supplier<Integer> supplier1 = ()->10;
  14.       System.out.println(supplier1.get());

  15.       //R apply(T t);
  16.       Function<String, Integer> function1 = (String s)->{return 20;};
  17.       Function<String, Integer> function2 = (s)->{return 20;};
  18.       Function<String, Integer> function3 = (s)-> 20;
  19.       Function<String, Integer> function4 = s->20;
  20.       System.out.println(function4.apply("Kalawati"));

  21. //  boolean test(T t);
  22.       Predicate<Integer> predicate1=(Integer i)->{return i%2==0;};
  23.       Predicate<Integer> predicate2=(i)->{return i%2==0;};
  24.       Predicate<Integer> predicate3=(i)->i%2==0;
  25.       Predicate<Integer> predicate4=i-> i%2==0;
  26.       System.out.println(predicate4.test(20));
  27.       System.out.println(predicate4.test(15));       
  28.     }
  29. }
Predicate chaining
It is possible to combine predicates into a single predicate by using the following methods.
  • and()
  • or()
  • negate()
  • These are exactly same as logical AND, OR, complement operator
Function chaining
It is also possible to combine functions into a single function by using the following methods
  • andThen()
  • compose()
Consumer chaining
It is also possible to combine consumers into a single consumer by using andThen() method.

Example
  1. import java.util.function.Consumer;
  2. import java.util.function.Function;
  3. import java.util.function.Predicate;
  4. public class FunctionChaining {
  5.     public static void main(String[] args) {
  6.         //Predicate  chaining
  7.         Predicate<Integer> p1 = i->i%2==0;
  8.         Predicate<Integer> p2 = i->i%3==0;
  9.         System.out.println(p1.negate().test(10));//false
  10.         System.out.println(p1.or(p2).test(15));//true
  11.         System.out.println(p1.or(p2).test(7));//false
  12.         System.out.println(p1.and(p2).test(12));//true
  13.         System.out.println(p1.and(p2).test(10));//false
  14.        
  15.         //Function chaining
  16.         Function<Integer, Integer> f1= i-> i+2;
  17.         Function<Integer,Integer> f2= i->i*3;
  18.         System.out.println(f1.andThen(f2).apply(4));//18
  19.         //Note in andThen() f1 will apply first then f2
  20.         System.out.println(f1.compose(f2).apply(4));//14
  21.         //Note in compose() f2 will apply first and then f2.

  22.         //Consumer chaining
  23.         Consumer<Integer> c1 = i->System.out.print(i+2);
  24.         Consumer<Integer> c2= i->System.out.print(i*2);
  25.         c1.andThen(c2).accept(4);//68
  26.     }
  27. }