Function接⼝
| Lambda表达式必须先定义接⼝,创建相关⽅法之后才可使⽤,这样做⼗分不便,其实java8已经内置了许多接⼝, 例如下⾯四个功能型接⼝,所以⼀般很少会由⽤户去定义新的函数式接⼝ |
| |
| Java8的最⼤特性就是函数式接⼝,所有标注了@FunctionalInterface注解的接⼝都是函数式接⼝ |
| |
| Java8 内置的四⼤核⼼函数式接⼝ |
| Consumer<T> : 消费型接⼝:有⼊参,⽆返回值 |
| void accept(T t); |
| |
| Supplier<T> : 供给型接⼝:⽆⼊参,有返回值 |
| T get(); |
| |
| Function<T, R> : 函数型接⼝:有⼊参,有返回值 |
| R apply(T t); |
| |
| Predicate<T> : 断⾔型接⼝:有⼊参,有返回值,返回值类型确定是boolean |
| boolean test(T t); |
Function接⼝使用
| # 测试1 |
| # 实现接口 |
| public class FunctionObj implements Function { |
| @Override |
| public Object apply(Object o) { |
| return o+"经过apply处理拼接上了"; |
| } |
| } |
| |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| test("小滴课堂",new FunctionObj()); |
| } |
| |
| public static void test(String input, Function function){ |
| System.out.println( function.apply(input)); |
| } |
| |
| } |
| |
| # 测试2 |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| |
| Function<Integer,Integer> func = p->p*100; |
| System.out.println(func.apply(10)); |
| } |
| |
| } |
BiFunction接⼝的使⽤
| # BiFunction Function只能接收⼀个参数,如果要传递两个参数,则⽤ BiFunction |
| |
| @FunctionalInterface |
| public interface BiFunction<T, U, R> { |
| R apply(T t, U u); |
| } |
| import java.util.function.BiFunction; |
| |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| System.out.println(operator(10,21,(a,b)->a+b)); |
| System.out.println(operator(10,2,(a,b)->a-b)); |
| System.out.println(operator(8,4,(a,b)->a*b)); |
| System.out.println(operator(10,2,(a,b)->a/b)); |
| } |
| |
| public static Integer operator(Integer a, Integer b, BiFunction<Integer,Integer,Integer> bf){ |
| return bf.apply(a,b); |
| } |
| |
| } |
Consumer接⼝的使⽤
| Consumer 消费型接⼝:有⼊参,⽆返回值 |
| 将 T 作为输⼊,不返回任何内容 |
| 调⽤⽅法:void accept(T t); |
| |
| @FunctionalInterface |
| public interface Consumer<T> { |
| void accept(T t); |
| } |
| import java.util.Arrays; |
| import java.util.List; |
| import java.util.function.Consumer; |
| |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| |
| |
| Consumer<String> consumer = obj->{ |
| System.out.println(obj); |
| System.out.println("调用短信接口发送短信,或者打印日志"); |
| }; |
| sendMsg("8888888",consumer); |
| |
| |
| sendMsg("8888888",obj->{ |
| System.out.println(obj); |
| System.out.println("调⽤短信接⼝发送短信,或者打印⽇志"); |
| }); |
| |
| |
| List<String> list = Arrays.asList("aaa","bbb"); |
| sendMsg2(list,obj->{ |
| System.out.println(obj); |
| System.out.println("调用短信接口发送短信,或者打印日志"); |
| }); |
| |
| } |
| |
| public static void sendMsg2(List<String> phone,Consumer<List<String>> consumer){ |
| consumer.accept(phone); |
| } |
| |
| public static void sendMsg(String phone,Consumer<String> consumer){ |
| consumer.accept(phone); |
| } |
| |
| } |
Supplier接⼝的使⽤
| Supplier: 供给型接⼝:⽆⼊参,有返回值 |
| T:出参类型;没有⼊参 |
| 调⽤⽅法:T get(); |
| |
| @FunctionalInterface |
| public interface Supplier<T> { |
| T get(); |
| } |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| Student student = newStudent(); |
| System.out.println(student.getName()); |
| } |
| |
| |
| public static Student newStudent(){ |
| Supplier<Student> supplier = ()-> { |
| Student student = new Student(); |
| student.setName("默认名称"); |
| return student; |
| }; |
| return supplier.get(); |
| } |
| |
| } |
| |
| |
| class Student{ |
| private String name; |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| } |
Predicate接⼝的使⽤
| Predicate: 断⾔型接⼝:有⼊参,有返回值,返回值类型确定是boolean |
| T:⼊参类型;出参类型是Boolean |
| 调⽤⽅法:boolean test(T t); |
| |
| @FunctionalInterface |
| public interface Predicate<T> { |
| boolean test(T t); |
| } |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| List<String> list = Arrays.asList("asdfdsfdsfds","xcvsdfsdf","sdfsdf","awwa","ad4432","dsfsdf"); |
| |
| List<String> results = filter(list,obj->obj.startsWith("a")); |
| |
| System.out.println(list); |
| |
| System.out.println(results); |
| } |
| |
| public static List<String> filter(List<String> list, Predicate<String> predicate){ |
| List<String> results = new ArrayList<>(); |
| for(String str:list){ |
| if(predicate.test(str)){ |
| results.add(str); |
| } |
| } |
| return results; |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术