Java 8 - Unary Operator & Primitive Versions
Unary Operator & Primitive Versions:
* If input and output both are same object then we should go for unary operator.
* It is the child of function.
eg.
Function<Integer, Integer> f = i -> i * i; - Dont go for function. Because input & output both are Integer.
Syntax:
interface UnaryOperator<T>
{
public T apply(T t);
}
Example:
class Test
{
public static void main(String[] args)
{
UnaryOperator<Integer> f=i->i*i;
System.out.println(f.apply(6));
}
}
Primitive Versions of Unary Operator:
1. IntUnaryOperator:
public int applyAsInt(int)
2. LongUnaryOperator:
public long applyAsLong(long)
3. DoubleUnaryOperator:
public double applyAsDouble(double)
Example:
class Test
{
public static void main(String[] args)
{
IntUnaryOperator f=i->i*i;
System.out.println(f.applyAsInt(6));
}
}
* If input and output both are same object then we should go for unary operator.
* It is the child of function.
eg.
Function<Integer, Integer> f = i -> i * i; - Dont go for function. Because input & output both are Integer.
Syntax:
interface UnaryOperator<T>
{
public T apply(T t);
}
Example:
class Test
{
public static void main(String[] args)
{
UnaryOperator<Integer> f=i->i*i;
System.out.println(f.apply(6));
}
}
Primitive Versions of Unary Operator:
1. IntUnaryOperator:
public int applyAsInt(int)
2. LongUnaryOperator:
public long applyAsLong(long)
3. DoubleUnaryOperator:
public double applyAsDouble(double)
Example:
class Test
{
public static void main(String[] args)
{
IntUnaryOperator f=i->i*i;
System.out.println(f.applyAsInt(6));
}
}
Comments
Post a Comment