Predefined functional interface - amresh087/newronaRepos GitHub Wiki
Functional Interface--> Functional interface are those whose contain single abstract method
Predicate --> Its take one argument for input type variable then perform some test condition after that its return boolean type because it is contain test()
interface Predicate<T>
{
Boolean test(T t);
}
Function --> Its take two argument first for input type and second for return type. Its contain apply method
interface Function<T,R>
{
R apply(T t);
}
Consumer --> it is contain accept method and it is not return any things
interface Consumer<T,R>
{
void accept(T t);
}
For example
package com.amresh.functional;
import java.util.function.Consumer;
public class ConsumerDemo {
public static void main(String[] args) {
Consumer<String> c=s->{System.out.println(s);};
c.accept("amresh");
c.accept("verma");
}
}
Supplier --> it is also predefined functional interface its take one argument. Which is Return type parameter where consumer take argument as Input type.
interface Supplier<R>
{
R get();
}
For example
Supplier<String> s=()->{
String s1[]={"amresh","ellen","anil","Ashok"};
int x=(int)(Math.random() * 3 + 1);
return s1[x];
};
Note--> Suppler return random value and here Math.random() is return between 0 to 9
package com.amresh.functional;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class ConsumerAndSupplierDemo {
public static void main(String[] args) {
Consumer<String> c=s->{System.out.println(s);};
c.accept("amresh");
c.accept("verma");
Supplier<String> s=()->{
String s1[]={"amresh","ellen","anil","Ashok"};
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());
System.out.println(s.get());
}
}