Saturday 22 September 2018



Predicate Functional Interface--------------------------------------------------------------------------

package govind;

import java.util.function.Predicate;

public class PredicateExample {

public static void main(String[] args) {
Predicate<Integer> p=I->I>10;
System.out.println(p.test(100));
System.out.println(p.test(5));
//System.out.println(p.test("govind ballabh khan"));//compile time error
}

}

o/p:------------------------

true
false

package govind;

import java.util.function.Predicate;

public class PredicateExampleStringLength {
public static void main(String[] args) {
Predicate<String> p=I->I.length()>5;
System.out.println(p.test("govind"));
System.out.println(p.test("khan"));
}

}
o/p:------------------------
true
false



package govind;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Predicate;

public class PredicateExampleCollectionEmpty {

public static void main(String[] args) {
Predicate<Collection> p=c->c.isEmpty();
ArrayList l1=new ArrayList<>();
l1.add("govind");
System.out.println(p.test(l1));
ArrayList l2=new ArrayList<>();
System.out.println(p.test(l2));


}

}


false
true


Function  FunctionalInterface example:--------------------------------------------------------------------------

govind;

import java.util.function.Function;

public class FunctionFunctionalInterface {
public static void main(String[] args) {
//calculate the length of String using Function functional interface
Function<String, Integer> f=s->s.length();
System.out.println(f.apply("govindkhan"));
System.out.println(f.apply("java"));
//calculate the square using Function functional interface
Function<Integer, Integer> k=i->i*i;
System.out.println(k.apply(10));
System.out.println(k.apply(5));


}

}


o/p:-----------------------------------


10
4
100
25


//Consumer FunctionalInterface example------------------------------------------------

package govind;

import java.util.function.Consumer;

public class ConsumerExample {

public static void main(String[] args) {
Consumer<String> p=c->System.out.println(c);
    p.accept("govind ballabh khan");
}

}


o/p:----------------------------------
govind ballabh khan


//Supplier FunctionalInterface example------------------------------------------------

package govind;

import java.util.function.Supplier;

public class SupplierExampleJava8 {
public static void main(String[] args) {
Supplier<String> s1=()->"govind ballabh Khan";
System.out.println(s1.get());
}
}



o/p:----------------------------------
govind ballabh khan

Supplier Example---------------------------------------------------------------------

package govind;

import java.util.function.Supplier;

public class SupplierExample {
public static void main(String[] args) {
Supplier<String> s=()->{String [] s1= {"govind","ballabh","khan","java"};
int x=(int)(Math.random()*3+1);
return s1[x];
};
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());
System.out.println(s.get());

}


}


o/p:----------------------------------------------------------------------------------------------------
khan
khan
ballabh
ballabh

No comments:

Post a Comment