Java 8 - Binary Operator & Primitive Versions
Binary Operator & Primitive Versions:
Function<T,R> - 1 input argument. input and output both are different type of object.
Unary Operator<T> - input and output both are same type of object
BiFunction<T,U,R> - 2 input argument
Binary Operator<T> - input and output all 3 are same type of object.
Syntax:
BinaryOperator<T>
public T apply(T,T)
BiFunction Example:
class Test
{
public static void main(String[] args)
{
BiFunction<String,String,String> f=(s1,s2)->s1+s2;
System.out.println(f.apply("durga","software"));
}
}
Binary Example:
class Test
{
public static void main(String[] args)
{
BinaryOperator<String> b=(s1,s2)->s1+s2;
System.out.println(b.apply("durga","software"));
}
}
Primitive versions of Binary Operator:
1. IntBinaryOperator
public int applyAsInt(int i,int j)
2. LongBinaryOperator
public long applyAsLong(long l1,long l2)
3. DoubleBinaryOperator
public double applyAsLong(double d1,double d2)
Example:
class Test
{
public static void main(String[] args)
{
IntBinaryOperator b=(i1,i2)->i1+i2;
System.out.println(b.applyAsInt(10,20));
}
}
Function<T,R> - 1 input argument. input and output both are different type of object.
Unary Operator<T> - input and output both are same type of object
BiFunction<T,U,R> - 2 input argument
Binary Operator<T> - input and output all 3 are same type of object.
Syntax:
BinaryOperator<T>
public T apply(T,T)
BiFunction Example:
class Test
{
public static void main(String[] args)
{
BiFunction<String,String,String> f=(s1,s2)->s1+s2;
System.out.println(f.apply("durga","software"));
}
}
Binary Example:
class Test
{
public static void main(String[] args)
{
BinaryOperator<String> b=(s1,s2)->s1+s2;
System.out.println(b.apply("durga","software"));
}
}
Primitive versions of Binary Operator:
1. IntBinaryOperator
public int applyAsInt(int i,int j)
2. LongBinaryOperator
public long applyAsLong(long l1,long l2)
3. DoubleBinaryOperator
public double applyAsLong(double d1,double d2)
Example:
class Test
{
public static void main(String[] args)
{
IntBinaryOperator b=(i1,i2)->i1+i2;
System.out.println(b.applyAsInt(10,20));
}
}
Comments
Post a Comment