IN BUILT FUNCTIONS
Consumer,Supplier,Function and Predicate all are available under java.util.function package.
Comparison of in built functions
How to call in built functions?
It is possible to combine predicates into a single predicate by using the following methods.
It is also possible to combine functions into a single function by using the following methods
It is also possible to combine consumers into a single consumer by using andThen() method.
Example
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?
- import java.util.function.Consumer;
- import java.util.function.Function;
- import java.util.function.Predicate;
- import java.util.function.Supplier;
- public class Test {
- public static void main(String[] args) {
- //void accept(T t);
- Consumer<String> consumer1 = (String s)->{System.out.println(s);};
- Consumer<String> consumer2=(s)->{System.out.println(s);};
- Consumer<String> consumer3=s->System.out.println(s);
- consumer3.accept("Amit");
- // T get();
- Supplier<Integer> supplier1 = ()->10;
- System.out.println(supplier1.get());
- //R apply(T t);
- Function<String, Integer> function1 = (String s)->{return 20;};
- Function<String, Integer> function2 = (s)->{return 20;};
- Function<String, Integer> function3 = (s)-> 20;
- Function<String, Integer> function4 = s->20;
- System.out.println(function4.apply("Kalawati"));
- // boolean test(T t);
- Predicate<Integer> predicate1=(Integer i)->{return i%2==0;};
- Predicate<Integer> predicate2=(i)->{return i%2==0;};
- Predicate<Integer> predicate3=(i)->i%2==0;
- Predicate<Integer> predicate4=i-> i%2==0;
- System.out.println(predicate4.test(20));
- System.out.println(predicate4.test(15));
- }
- }
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
It is also possible to combine functions into a single function by using the following methods
- andThen()
- compose()
It is also possible to combine consumers into a single consumer by using andThen() method.
Example
- import java.util.function.Consumer;
- import java.util.function.Function;
- import java.util.function.Predicate;
- public class FunctionChaining {
- public static void main(String[] args) {
- //Predicate chaining
- Predicate<Integer> p1 = i->i%2==0;
- Predicate<Integer> p2 = i->i%3==0;
- System.out.println(p1.negate().test(10));//false
- System.out.println(p1.or(p2).test(15));//true
- System.out.println(p1.or(p2).test(7));//false
- System.out.println(p1.and(p2).test(12));//true
- System.out.println(p1.and(p2).test(10));//false
- //Function chaining
- Function<Integer, Integer> f1= i-> i+2;
- Function<Integer,Integer> f2= i->i*3;
- System.out.println(f1.andThen(f2).apply(4));//18
- //Note in andThen() f1 will apply first then f2
- System.out.println(f1.compose(f2).apply(4));//14
- //Note in compose() f2 will apply first and then f2.
- //Consumer chaining
- Consumer<Integer> c1 = i->System.out.print(i+2);
- Consumer<Integer> c2= i->System.out.print(i*2);
- c1.andThen(c2).accept(4);//68
- }
- }