Lamda expressions
Lamda Expressions in Java:
Default Methods in Interface: clich here
- without effecting implementation classes if we want to add new methods to interface then we default methods
- we can overide default methods in implementation classes or we can use deault implentation provided in interface it is up to you.
- Default methods shold not be the methods of object classes.
- Diamond problem can occur if a class implemts 2 inerfaces having same methods, the solution is discussed in above link.
Static Methods in Interface: click here
- static methods are not part of implementation classes by deault.So you cannot override static methods in implentation classes.
- Since it is not part of implementation classes, you cannot call it by creating object of implementation class.
- Only way to call static method in interface is.InterfaceName.methodName()
- To define general utily methods which never talks about objects we use static methods inside interface
Functional Interface: click here
- An interface with excatly one abstract method is called fnational Interface
- There can be any number of static or default methods.
Write Lamds Expressions:
Predefined Functional Interfaces:
Predicate:
interface Predicate<T t>{
public boolean test(T t){
}
- We use predicate for condiional checks i.e it accepts any input type and returns boolen value only.
Predicate<Integer> p = num -> num%2==0;
- we can call above lamda expression using p.test(anyNum) any number of times.
- Its possible to join predicates using and(), or() and negate() methods:
Predicate<Integer> p1 = num -> num%2==0;
Predicate<Integer> p2 = num -> num>10;
Function:
interface Function<T t,R r>{
public R apply(T t){
}
- We use function when you have a requirement to accept one input and return any value.
Function<String,Integer> f = s -> s.length();
- we can call above lamda expression any numer of times using f.apply("any string")
- Other default methods like andThen() & compose() are used for chaining i.e to use output of one method return value to the other.
Function<Integer,Integer> f2= i2->i2*i2;
1. f1.andThen(f2).apply(2)
It will first perform f1 and then f2 i.e first doubles and then squares, so we get result as 16.
2. f1.compose(f2).apply(2)
It is quite opposite to above i.e first performs f2 and on that result it performs f1, so we get result as 8.
Consumer:
interface Consumer<T t>{
public void accept(T t){
}
- By the name we can guess that it accepts any input values but didn't return any thing i.e void.
Eg: Print some thing to console
Consumer<String > c= str -> System.out.println(str);
- we can call above lamda expression any numer of times using c.accept("any string")
- It has only one default method used for chaining i.e andThen, which is used exactly above.
Supplier:
- By the name we can guess that it does not accepts any input values but returns value.
- we can call above lamda expression any numer of times using s.get()
- There are no other methods for chaining
BiFunctionalInterfaces
- Above functional interfaces only accept one input parameter, below mentioned functional interfaces are exactly same as above ones but accepts 2 arguments.
- BiPredicate
- BiFunction
- BiConsummer
Primitive Functional Interfaces
- There are performance issues with normal functional Interfaces due to auto-boxing and unboxing which is auto mmatically performed by jvm, so we use primitive version of functional interfaces.
- Example consider below example, if we want to perform square of number, then we pass in primitive type to apply function, but this interface accepts Integer, so autoboxing is performed by jvm and similary for permoning product operation aut0-unboxing is performed to convert back o int and finally the result is auto boxed to Integer.This is performance issue so we use primitive versions of same functional interfaces.
- Refer java8 documentation here
- Input type as mentioned in name and return type can be any
- Input type as mentioned before to and return type after to
- Input type can be any and return type after To
- For Bi-Functions, inputs can be any types but return type menioned after to
- Input type as mentioned in the name return type is anyway void.
- IntConsumer
- DoubleConsumer
- LongConsumer
- for BiConsumer one arument is any thing and other is fixed as mentioned in name.
- ObjIntConsumer<T> - (void accept(T t,int value) )
- ObjLongConsumer<T>
- ObjDoubleConsumer<T>
- The return type
- BooleanSupplier
- IntSupplier
- LongSupplier
- DoubleSupplier
- This is child interface of Function interface
- It is just like function interface with both input and return type same.
- even abstract methos remains same i.e apply()
- IntUnaryOperator
- LongUnaryOperator
- DoubleUnaryOperator
- The best way to write square of a number is
- IntUnaryOpeator f=num->num*num;
- we can call the same using f.applyAsInt(3)
- This is child interface of Bi-Function interface
- It is just like Bi-function interface with 2 input parametrs and return type also same.
- even abstract methos remains same i.e apply()
- IntBinaryOperator
- LongBinaryOperator
- DoubleBinaryOperator
- The best way to write sum of 2 numbers is
- IntBinaryOpeator f=num->num+num;
- we can call the same using f.applyAsInt(3,4)
Comments
Post a Comment