lambda表达式
函数式接口#
- Supplier 代表一个输出
- Consumer 代表一个输入
- BiConsumer 代表两个输入
- Function代表一个输入,一个输出(一般输入和输出是不同类型的)
- UnaryOperator 代表一个输入,一个输出(输入和输出是相同类型的)
- BiFunction代表一个输入,一个输出(一般输入和输出是不同类型的)
- BinaryOperator 代表一个输入,一个输出(输入和输出是相同类型的)
public class LambdaTest{ public static void main(String[] args){ Runnable runnable = new Runnable(){ @Override public void run(){ // coding... } } runnable.run(); Runnable runnable2 = ()->{ // coding... }; runnable2.run(); Runnable runnable3 = ()-> // coding runnable3.run(); Callable c1 = new Callable(){ @Override public String call() throws Exception{ return ""; } }; c1.call(); Callable<String> c2 = ()->{return "";}; c2.call(); Callable<String> c3 = ()->""; c3.call(); Function<String, Integer> f1 = (str)->{return str.length();}; Supplier<String> s1 = ()->{return "";}; Supplier<String> s2 = ()->""; s1.get(); Consumer<String> c11 = (str)->System.out.println(str); c11.accept("sth"); Runnable runnable1 = ()->{int i = get();System.out.println(i)}; runnable1.run(); Runnable runnable2 = ()->{int i = exec();System.out.println(i)}; runnable2.run(); Runnable runnable3 = ()->100; runnable3.run(); Runnable runnable4 = ()->""; runnable4.run(); LamabdInterface li1 = ()->get(); LamabdInterface li2 = ()->find(); LamabdInterface li3 = ()->100; LamabdInterface li4 = ()->"abc"; LamabdInterface li5 = ()->true?1:0; BiFunction<String, String, Integer> bf = (a,b)->a.length() + b.length(); bf.apply("", "") List<String> list = Arrays.aslist("a", "b", "c"); for(String s : list){ System.out.println(s); } // 上面和下面两种写法都可以输入s list.forEach(System.out::println); } static int get(){ return 1; } static int find(){ return “find"; } static void exec(){ } }
public class Test{ static String put(){ return "put"; } public static int getSize(int size){ return size; } public static void main(String[] args){ Supplier<String> s1 = ()->Test.put(); System.out.println(s1.get()); Supplier<String> s2 = Test::put; System.out.println(s2.get()); Supplier<String> s3 = Fun::test; System.out.println(s3.get()); Consumer <Integer> c1 = Test::getSize; c1.accept("123"); Consumer <Integer> c2 = (size)->Test::getSize(size); Function<String, String> f1 = str->str.toUpperCase(); Function<String, String> f2 = str->Test.toUpperCase(str); Function<String, String> f3 = str->Test::toUpperCase; Function<String, String> f4 = Fun::toUpperCase; f1.apply("test"); f2.apply("test"); f3.apply("test"); f4.apply("test"); BiFunction<String, String, Integer> bf = (a,b)->a.length() + b.length(); BiFunction<String, String, Integer> bf2 = Test::getLength; bf.apply("abc", "def") bf2.apply("abc", "def") } class Fun{ public static String test(){ return "test"; } public static String toUpperCase(String str){ return str.toUpperCase(); } public static Integer getLength(String str, String str2){ return str.length() + str2.length(); } } }
作者:BigBender
出处:https://www.cnblogs.com/BigBender/p/14214333.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2019-12-31 一元函数微分学概念与计算(一)
2019-12-31 立体几何初步--立体几何体
2019-12-31 高中数学--幂函数
2019-12-31 对数与对数函数
2019-12-31 基本初等函数(Ⅰ)
2019-12-31 高中数学--函数与方程
2019-12-31 高中数学--一次函数和二次函数