赞
踩
目录
3.2 @functionalinterface注解 源码分析

3.1 什么是函数式接口
如果一个接口中只包含一个抽象方法,这个接口称为函数式接口
如果一个接口包含,一个抽象方法、一个默认方法、一个静态方法,这个接口也是函数式接口
3.2 @functionalinterface注解 源码分析
一个标记性注解
在定义接口是函数式接口时,可以使用 @functionalinterface 注解进行标注,该注解会在编译器对这个接口进行检查
如果使用@functionalinterface 注解来定义接口,编译器将会强制检查该接口,是否只有一个抽象方法,否则会报错。
3.3 Lambda表达式和函数式接口关系
Lambda表达式就是一个函数式接口的实例
如果一个对象是函数式接口的实例,那么该对象就可以用Lambda表达式,用匿名实现类表示的地方都可以用Lambda表达式替换
3.4 使用函数式接口
可以使用JDK中内置的函数式接口,也可以自定义函数式接口
案例1:
先定义一个函数式接口
@FunctionalInterface public interface MyFun<T> { 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 MyFun<String>() { @Override public 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(MyFun<String> function, String str) { return function.getValue(str); } }运行结果:
3.5 内置函数式接口
在JDK8中内置了以下四大核的函数式接口
Supplier接口、Consumer接口、Function接口、Predicate接口
四大核的函数式接口区别
函数式接口 参数类型 返回类型 使用说明 Consumer<T> T void 对类型为T的对象应用操作,
包含方法:void accept(T t)
Supplier<T> 无 T 返回类型为T的对象,
包含方法:T get()
Function<T,R> T R 对类型为T的对象应用操作,
并返回结果为R类型的对象。包含方法:Rapply(T t)
Predicate<T> T boolean 确定类型为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 Supplier<Integer>() { @Override public 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(Supplier<Integer> 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 Consumer<String>() { @Override public void accept(String s) { System.out.println(s.toUpperCase()); } }); transform(s -> System.out.println(s.toUpperCase())); } public static void transform(Consumer<String> 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 Function<String, Integer>() { @Override public 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(Function<String,Integer> function) { Integer value = function.apply("10"); return value + 5; } public static int calculate1(Function<String,Integer> f1,Function<Integer,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(Predicate<String> predicate,String name) { System.out.println(predicate.test(name) ? "超过3个字" : "没有超过3个字"); } /** * 使用Lambda表达式判断一个字符中,包含A,包含B * 使用了Predicate 中的 and 与 * default Predicate<T> and(Predicate<? super T> other) { * Objects.requireNonNull(other); * return (t) -> test(t) && other.test(t); * } */ private static void testAnd(Predicate<String> p1,Predicate<String> p2,String str) { System.out.println(p1.and(p2).test(str) ? "yes" : "no"); } /** * 使用Lambda表达式判断一个字符中,可能包含Z 或者 可能 包含Y * 使用了Predicate 中的 or 或 * default Predicate<T> or(Predicate<? super T> other) { * Objects.requireNonNull(other); * return (t) -> test(t) || other.test(t); * } */ private static void testOr(Predicate<String> p1,Predicate<String> p2,String str) { System.out.println(p1.or(p2).test(str) ? "可能包含Z 或者 可能 包含Y" : "不包含Z 不包含Y"); } /** * 使用Lambda表达式判断一个字符中,可能不包含Q * 使用了Predicate 中的 * default Predicate<T> negate() { * return (t) -> !test(t); * } * */ private static void testNegate(Predicate<String> p1, String str) { System.out.println(p1.negate().test(str) ? "不包含Q" : "包含Q"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。