一、函数式接口
1.1 简介
- 首先的是一个接口
- 接口内有且只有一个抽象方法
- 为防止破坏函数式接口,最好是在接口上使用
- @FunctionalInterface注解修饰
定义一个函数接口
| package com.xxx; |
| |
| @FunctionalInterface |
| public interface Inner3 { |
| |
| void showInfo(); |
| |
| default void show() { |
| } |
| } |
| |
1.1.1 函数接口作为方法的参数
1.1.2 函数接口作为方法的返回值
1.1.3 jdk提供的函数型接口作为方法的参数
1.2 供给型接口-Supplier
| package com.xxx; |
| |
| import java.util.function.Supplier; |
| |
| public class SupplierTest { |
| public static void main(String[] args) { |
| String str = getStr(() -> { |
| return "函数式接口Supplier".substring(0, 1); |
| }); |
| |
| System.out.println(str); |
| } |
| |
| public static String getStr(Supplier<String> supplier){ |
| return supplier.get(); |
| } |
| } |
| |
1.3 消费型接口-Consumer
方法名称 |
方法描述 |
void accept(T t) |
提供一个消费的方法 |
default Consumer andThen(Consumer<? super T> after) |
连接多个消费型接口 |
| package com.xxx.lambda.function; |
| |
| import java.util.function.Consumer; |
| |
| public class ConsumerTest { |
| public static void main(String[] args) { |
| showInfo("函数式接口-消费型-Consumer",(String con) -> { |
| System.out.println(con.substring(0, 1)); |
| }); |
| } |
| |
| public static void showInfo(String s, Consumer<String> con) { |
| con.accept(s); |
| } |
| } |
| |
| package com.xxx.lambda.function; |
| |
| import java.util.function.Consumer; |
| |
| public class ConsumerTest2 { |
| public static void main(String[] args) { |
| getStr("asdfgQWERTY",(String con1) -> { |
| |
| },(String con2) -> { |
| |
| }); |
| } |
| |
| public static void getStr(String s, Consumer<String> consumer1, Consumer<String> consumer2){ |
| |
| |
| consumer1.andThen(consumer2).accept(s); |
| } |
| } |
| |
1.4 断言型接口-Predicate
方法名称 |
方法描述 |
default Predicate and(Predicate<? super T> other) |
并且&& |
default Predicate negate() |
非! |
default Predicate or(Predicate<? super T> other) |
或者 |
boolean test(T t) |
用于判断 |
| package com.xxx.test01; |
| |
| import java.util.function.Predicate; |
| |
| public class PredicateTest { |
| public static void main(String[] args) { |
| boolean flag = isFlag("最靓的仔", (String s) -> { |
| return s.length() > 5; |
| }); |
| |
| System.out.println(flag); |
| } |
| |
| public static boolean isFlag(String s, Predicate<String> pre){ |
| return pre.test(s); |
| } |
| } |
| |
| package com.xxx; |
| |
| import java.util.function.Predicate; |
| |
| |
| |
| |
| public class PredicateTest2 { |
| public static void main(String[] args) { |
| boolean flag = isFlag("他想高歌一首", (String p1) -> { |
| return p1.length() > 5; |
| }, (String p2) -> { |
| return p2.contains("歌"); |
| }); |
| |
| System.out.println(flag); |
| } |
| |
| public static boolean isFlag(String s, Predicate<String> p1, Predicate<String> p2) { |
| return p1.negate().test(s); |
| } |
| } |
| |
| package com.xxx; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.function.Predicate; |
| |
| |
| |
| |
| |
| public class PredicateTest3 { |
| public static void main(String[] args) { |
| String[] ss={"迪丽热巴","古力娜扎","马尔扎哈"}; |
| |
| List<String> list = showInfo(ss, (String s1) -> { |
| return s1.length() == 4; |
| }, (String s2) -> { |
| return s2.contains("迪"); |
| }); |
| System.out.println(list); |
| } |
| |
| public static List<String> showInfo(String[] arrays, Predicate<String> pre1, Predicate<String> pre2) { |
| List<String> list = new ArrayList<>(); |
| |
| for (String array : arrays) { |
| if (pre1.and(pre2).test(array)) { |
| list.add(array); |
| } |
| } |
| |
| return list; |
| |
| } |
| } |
| |
1.5 函数型接口-Function
方法名称 |
方法描述 |
default Function<T,V> andThen(Function<? super R,? extends V> after) |
用于连接多个函数型接口 |
R apply(T t) |
用于来进行转换 |
| package com.xxx; |
| |
| import java.util.function.Function; |
| |
| |
| |
| |
| public class FunctionTest { |
| public static void main(String[] args) { |
| Integer in = showInfo("123", (String s) -> { |
| return Integer.parseInt(s); |
| }); |
| System.out.println(); |
| } |
| |
| public static Integer showInfo(String s, Function<String, Integer> fun) { |
| return fun.apply(s); |
| } |
| } |
| |
| package com.xxx; |
| |
| import java.util.function.Function; |
| |
| |
| |
| |
| public class FunctionTest2 { |
| public static void main(String[] args) { |
| String s = showInfo("123", Integer::parseInt, (Integer i2) -> { |
| return i2 + ""; |
| }); |
| System.out.println(s); |
| |
| } |
| |
| public static String showInfo(String s, Function<String, Integer> fun1, Function<Integer,String> fun2) { |
| return fun1.andThen(fun2).apply(s); |
| } |
| } |
| |
本文作者:Ritchie里其
本文链接:https://www.cnblogs.com/wang-zeyu/p/16829997.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步