渭南微信小程序网站建设,门户网站英文,公司网站建设怎么做,私有云笔记 wordpress目录
3.函数式接口
思维导图
3.1 什么是函数式接口
3.2 functionalinterface注解 源码分析
3.3 Lambda表达式和函数式接口关系
3.4 使用函数式接口
3.5 内置函数式接口
四大核的函数式接口区别
3.5.1 Supplier 函数式接口源码分析
3.5.2 Supplier 函数式接口使用
3.…目录
3.函数式接口
思维导图
3.1 什么是函数式接口
3.2 functionalinterface注解 源码分析
3.3 Lambda表达式和函数式接口关系
3.4 使用函数式接口
3.5 内置函数式接口
四大核的函数式接口区别
3.5.1 Supplier 函数式接口源码分析
3.5.2 Supplier 函数式接口使用
3.5.3 Consumer 函数式接口源码分析
3.5.4 Consumer 函数式接口使用
3.5.5 Function 函数式接口源码分析
3.5.6 Function 函数式接口使用
3.5.7 Predicate 函数式接口源码分析
3.5.8 Predicate 函数式接口使用 3.函数式接口
思维导图 3.1 什么是函数式接口 如果一个接口中只包含一个抽象方法这个接口称为函数式接口 如果一个接口包含一个抽象方法、一个默认方法、一个静态方法这个接口也是函数式接口 3.2 functionalinterface注解 源码分析 一个标记性注解 在定义接口是函数式接口时可以使用 functionalinterface 注解进行标注该注解会在编译器对这个接口进行检查 如果使用functionalinterface 注解来定义接口编译器将会强制检查该接口是否只有一个抽象方法否则会报错。 3.3 Lambda表达式和函数式接口关系 Lambda表达式就是一个函数式接口的实例 如果一个对象是函数式接口的实例那么该对象就可以用Lambda表达式用匿名实现类表示的地方都可以用Lambda表达式替换 3.4 使用函数式接口 可以使用JDK中内置的函数式接口也可以自定义函数式接口 案例1 先定义一个函数式接口 FunctionalInterface
public interface MyFunT {T getValue(T t);
}写一个实现类FunctionInterfaceDemo1进行对给定字符串转为大写 /*** 函数式接口使用*/
public class FunctionInterfaceDemo1 {public static void main(String[] args) {//需求 使用函数式接口将给定的字符转为大写//1.使用原始方法System.out.println(toUpperCase(hello));//2.使用函数式接口String result1 toLowerCase(new MyFunString() {Overridepublic String getValue(String s) {return s.toUpperCase();}}, hello);System.out.println(result1: result1);String result2 toLowerCase( s - s.toUpperCase(), hello);System.out.println(result2: result2);}// 定义一个方法将给定的字符串转为大写public static String toUpperCase(String str) {return str.toUpperCase();}/*** 将给定的字符串转为大写* param function 函数式接口* param str 给定字符串* return*/public static String toLowerCase(MyFunString function, String str) {return function.getValue(str);}
} 运行结果 3.5 内置函数式接口 在JDK8中内置了以下四大核的函数式接口 Supplier接口、Consumer接口、Function接口、Predicate接口 四大核的函数式接口区别 函数式接口参数类型返回类型使用说明ConsumerTTvoid 对类型为T的对象应用操作 包含方法:void accept(T t) SupplierT无T 返回类型为T的对象 包含方法:T get() FunctionT,RT R 对类型为T的对象应用操作 并返回结果为R类型的对象。包含方法:Rapply(T t) PredicateTTboolean 确定类型为T的对象是否满足某约束 并返回布尔值。包含方法: booleantest(T t) 3.5.1 Supplier 函数式接口源码分析 3.5.2 Supplier 函数式接口使用 案例 /*** JDK内置Supplier函数式接口的使用*/
public class SupplierDemo1 {public static void main(String[] args) {getMax(new SupplierInteger() {Overridepublic Integer get() {//1.定义一个数组int[] arr {12,23,34,94,555,60,27,38,99};//2.对数组进行排序Arrays.sort(arr);//3.将最大值返回return arr[arr.length - 1];}});getMax(() -{//1.定义一个数组int[] arr {12,23,34,94,555,60,27,38,99};//2.对数组进行排序Arrays.sort(arr);//3.将最大值返回return arr[arr.length - 1];});}private static void getMax(SupplierInteger supplier) {int max supplier.get();System.out.println(max);}
} 运行结果 3.5.3 Consumer 函数式接口源码分析 消费一个数据其数据类型由泛型参数决定 Consumer 函数式接口可以通过accept()方法参数传递过来的数据进行处理 有参无返回接口 3.5.4 Consumer 函数式接口使用 案例 /*** JDK内置 消费型函数式接口 Consumer 使用*/
public class ConsumerDemo {public static void main(String[] args) {transform(new ConsumerString() {Overridepublic void accept(String s) {System.out.println(s.toUpperCase());}});transform(s - System.out.println(s.toUpperCase()));}public static void transform(ConsumerString consumer) {consumer.accept(hello Consumer);}
} 3.5.5 Function 函数式接口源码分析 根据一个类型的数据得到另外一个类型的数据有参有返回 Function转换型接口对apply()方法传入的T类型数据进行处理返回R类型的结果 3.5.6 Function 函数式接口使用 案例 /*** JDk内置功能型接口 Function 使用*/
public class FunctionDemo {public static void main(String[] args) {System.out.println(calculate(new FunctionString, Integer() {Overridepublic Integer apply(String s) {return Integer.parseInt(s);}}));System.out.println(calculate( s- Integer.parseInt(s)));System.out.println(calculate1(s- Integer.parseInt(s), i - i 5));}public static int calculate(FunctionString,Integer function) {Integer value function.apply(10);return value 5;}public static int calculate1(FunctionString,Integer f1,FunctionInteger,Integer f2) {Integer value2 f1.andThen(f2).apply(10);return value2;}
} 3.5.7 Predicate 函数式接口源码分析 需要对某种类型的数据进行判断从而得到一个Boolean值结果常用于条件判断场景 3.5.8 Predicate 函数式接口使用 /*** JDK内置断言函数式接口 Predicate 使用*/
public class PredicateDemo {public static void main(String[] args) {testName(s - s.length() 3,张三);testAnd(s - s.contains(A),s - s.contains(B),ABC);testAnd(s - s.contains(A),s - s.contains(B),Abc);testOr(s - s.contains(Z),s - s.contains(Y),Zxc);testOr(s - s.contains(Z),s - s.contains(Y),zxc);testNegate(s - s.contains(Q),iQ);testNegate(s - s.contains(Q),asd);}/*** 使用Lambda表达式判断 名字长度是否超过3个字*/private static void testName(PredicateString predicate,String name) {System.out.println(predicate.test(name) ? 超过3个字 : 没有超过3个字);}/*** 使用Lambda表达式判断一个字符中包含A包含B* 使用了Predicate 中的 and 与* default PredicateT and(Predicate? super T other) {* Objects.requireNonNull(other);* return (t) - test(t) other.test(t);* }*/private static void testAnd(PredicateString p1,PredicateString p2,String str) {System.out.println(p1.and(p2).test(str) ? yes : no);}/*** 使用Lambda表达式判断一个字符中可能包含Z 或者 可能 包含Y* 使用了Predicate 中的 or 或* default PredicateT or(Predicate? super T other) {* Objects.requireNonNull(other);* return (t) - test(t) || other.test(t);* }*/private static void testOr(PredicateString p1,PredicateString p2,String str) {System.out.println(p1.or(p2).test(str) ? 可能包含Z 或者 可能 包含Y : 不包含Z 不包含Y);}/*** 使用Lambda表达式判断一个字符中可能不包含Q* 使用了Predicate 中的* default PredicateT negate() {* return (t) - !test(t);* } **/private static void testNegate(PredicateString p1, String str) {System.out.println(p1.negate().test(str) ? 不包含Q : 包含Q);}
}