Java 8 - primitive Functional interfaces for Predicate
The following are various primitive Functional interfaces for Predicate.
1.IntPredicate-->always accepts input value of int type
2.LongPredicate-->always accepts input value of long type
3.DoublePredicate-->always accepts input value of double type
interface IntPredicate
{
public boolean test(int i);
//default methods: and(),or(),negate()
}
interface LongPredicate
{
public boolean test(long l);
//default methods: and(),or(),negate()
}
interface DoublePredicate
{
public boolean test(double d);
//default methods: and(),or(),negate()
}
Example:
class Test
{
public static void main(String[] args)
{
int[] x ={0,5,10,15,20,25};
IntPredicate p=i->i%2==0;
for (int x1 : x)
{
if(p.test(x1))
{
System.out.println(x1);
}
}
}
}
In the above example, autoboxing and autounboxing won't be performed internally.Hence performance wise improvements are there.
1.IntPredicate-->always accepts input value of int type
2.LongPredicate-->always accepts input value of long type
3.DoublePredicate-->always accepts input value of double type
interface IntPredicate
{
public boolean test(int i);
//default methods: and(),or(),negate()
}
interface LongPredicate
{
public boolean test(long l);
//default methods: and(),or(),negate()
}
interface DoublePredicate
{
public boolean test(double d);
//default methods: and(),or(),negate()
}
Example:
class Test
{
public static void main(String[] args)
{
int[] x ={0,5,10,15,20,25};
IntPredicate p=i->i%2==0;
for (int x1 : x)
{
if(p.test(x1))
{
System.out.println(x1);
}
}
}
}
In the above example, autoboxing and autounboxing won't be performed internally.Hence performance wise improvements are there.
Comments
Post a Comment