Java 8 - Primitive Type Functional Interface for Function

Primitive Type Functional Interface for Function:

Drawback Program:

Function<Integer, Integer> f = i -> i*i;
System.out.println(f.apply(5));  // primitive type convertion will happend to run the above step. (So Autoboxing and Auto Unboxing will be happend)

Functions are ,

1. IntFunction: can take int type as input and return any type
public R apply(int i);
2. LongFunction: can take long type as input and return any type
public R apply(long i);
3. DoubleFunction: can take double type as input and return any type
public R apply(double d);
4. ToIntFunction: It can take any type as input but always returns int type
public int applyAsInt(T t)
5. ToLongFunction: It can take any type as input but always returns long type
public long applyAsLong(T t)
6. ToDoubleFunction: It can take any type as input but always returns double type
public int applyAsDouble(T t)
7. IntToLongFunction: It can take int type as input and returns long type
public long applyAsLong(int i)
8. IntToDoubleFunction: It can take int type as input and returns long type
public double applyAsDouble(int i)
9. LongToIntFunction: It can take long type as input and returns int type
public int applyAsInt(long i)
10. LongToDoubleFunction: It can take long type as input and returns double type
public int applyAsDouble(long i)
11. DoubleToIntFunction: It can take double type as input and returns int type
public int applyAsInt(double i)
12. DoubleToLongFunction: It can take double type as input and returns long type
public int applyAsLong(double i)
13. ToIntBiFunction: return type must be int type but inputs can be anytype
public int applyAsInt(T t, U u)
14. ToLongBiFunction: return type must be long type but inputs can be anytype
public long applyAsLong(T t, U u)
15. ToDoubleBiFunction: return type must be double type but inputs can be anytype
public double applyAsDouble(T t, U u)

Using Function: (Performance is Slow)
class Test
{
public static void main(String[] args)
{
Function<String,Integer> f=s->s.length();
System.out.println(f.apply("vinoth"));
}
}

Using IntFunction: (Performance is High)
class Test
{
public static void main(String[] args)
{
ToIntFunction<String> f=s->s.length();
System.out.println(f.applyAsInt("vinoth"));
}

Comments

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab