目录
常用4种函数式接口
Supplier接口
Consumer接口
Function接口
Predicate 接口
常用的函数式接口主要在java.util.function包下,函数式接口是Lambda表达式使用的前提,而Lambda表达式不关心接口名,抽象方法名,只关心抽象方法的参数列表和返回值类型,因此,为了让Lambda表达式使用更方便,jdk提供了大量函数式接口。(常用的接口:Supplier接口、Consumer接口、Function接口、Predicate接口等)
常用4种函数式接口
本篇文章只介绍常用的Supplier接口、Consumer接口、Function接口、Predicate接口四种接口。
Supplier接口
@FunctionalInterface
public interface Supplier<T>{ public abstract T get(); }
对应的Lambda表达式需要“对外提供"一个符合泛型类型的对象数据。供给型接口,通过Supplier接口中的get方法可以得到一个值,无参有返回的接口。
//获取数组中最大的值
public class GetMaxValue {
public static void main(String[] args) {
getMax(()->{
int[] arr = {13,89,45,32,80};
Arrays.sort(arr); //排序
return arr[arr.length - 1]; //数组默认升序排列 最后一个就是最大值
});
}
private static void getMax(Supplier<Integer> sup) {
Integer max = sup.get();
System.out.println("max : "+max);
}
}
Consumer接口
@FunctionalInterface
public interface Consumer<T>{
public abstract void accept<T t);
}
Consumer<T>接口不是生产一个数据,而是消费一个数据,和Supplier接口相反,其数据类型由泛型参数决定。
//将传入的值转化为大写或小写
public class GetMaxValue {
public static void main(String[] args) {
//Consumer将值转化为大写
changeMax((String s)->{
System.out.println(s.toUpperCase());//将传入的值转为大写
System.out.println(s.toLowerCase());//将传入的值转为小写
});
//Consumer接口中的andThen方法 先打印大写 然后打印小写
andThenWith(s -> System.out.print(s.toLowerCase() +" ---- "),s -> System.out.print(s.toUpperCase()));
}
private static void changeMax(Consumer<String> con){
String t = "abcDeH";//传入的值
con.accept(t);
}
//Consumer接口中的默认方法andThen
private static void andThenWith(Consumer<String> c1,Consumer<String> c2){
c2.andThen(c1).accept("HelloWorld");
}
}
Function接口
@FunctionalInterface
public interface Function<T,R>{
public abstract R apply(T t);
}
用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件,后置称为后置条件,有参数有返回值。Function转换型接口,对apply方法传入的T类型数据进行处理,返回R类结果,有参有返回的接口。
//将String类型转换为Integer类型
public class GetMaxValue {
public static void main(String[] args) {
//Function接口将值转化为数字
changeSToI(s -> {
return Integer.parseInt(s);
});
//Function接口将字符串转换为数字并乘以10
funAndThen(s -> {
//将传入的值转换为int类型
return Integer.parseInt(s);
},s->{
//将上一步的结果乘以10返回
return s * 10;
});
}
//Function接口将一个字符串转化为数字
private static void changeSToI(Function<String,Integer> fun){
Integer fun = fun.apply("58");
System.out.println(" in "+(fu + 10));
}
//Function接口中的andThen方法
private static void funAndThen(Function<String,Integer> fun1,Function<Integer,Integer> fun2){
Integer value = fun1.andThen(fun2).apply("56");
System.out.println("value : "+value);
}
}
Predicate 接口
@FunctionalInterface
public interface Predicate<T>{
public abstract boolean test(T t);
}
//该接口用于做判断,返回boolean类型的值
有时我们需要对某种数据类型进行判断,从而得到一个boolean值结果,使用Predicate接口即可,该接口用于做判断,返回boolean类型的值。
public class GetMaxValue {
public static void main(String[] args) {
//Predicate接口用于判断 返回Boolean值
tesetPre(s -> s.length() >= 3,"太极张三丰");
//Predicate接口判断是否包含 and 其他两个值
testContain( s->
s.contains("H")
, s->
s.contains("W")
,"HelloWorld");
//Predicate接口包含H或W
testContain2(s->s.contains("H"),s->s.contains("W"),"HelloBlus");
//不包含H
testContain3(s->s.contains("H"),"youyouku");
}
//Predicate接口判断返回一个Boolean值
private static void tesetPre(Predicate<String> pre,String name){
boolean bool = pre.test(name);
System.out.println("如果名字超过三个则名字太长 : "+bool);
}
//使用Predicate接口and方法判断一个字符串既包含H也包含W
private static void testContain(Predicate<String> pre1,Predicate<String> pre2,String str){
boolean test = pre1.and(pre2).test(str);
System.out.println("该值是否既包含其他两值 : "+test);
}
//使用Predicate接口or方法判断一个字符串包含H或包含W
private static void testContain2(Predicate<String> pre1,Predicate<String> pre2,String str){
boolean test = pre1.or(pre2).test(str);
System.out.println("包含H或W :" +test);
}
//使用Predicate中negate默认方法判断字符串不包含
private static void testContain3(Predicate<String> pre1,String str){
boolean test = pre1.negate().test(str);
System.out.println("不包含 : "+test);
}
}