- 认识lambda
package com.yu.lambda; public class TestLambda { public static void main(String[] args) throws Exception { new Thread(() -> System.out.println("OK")).start(); IBookNumber book = (i) -> i * 2; System.out.println(book.isbn(897654)); System.out.println(book.name("a")); } } @FunctionalInterface interface IBookNumber { int isbn(int i); default String name(String name) { return "java " + name; } }
OK 1795308 java a
- 函数接口
package com.yu.lambda; import java.text.DecimalFormat; import java.util.function.Function; public class TestFunction { public static void main(String[] args) throws Exception { A a = new A(1234567890); Function<Integer, String> mf = i -> new DecimalFormat("#,###").format(i); a.pM(mf.andThen(s -> " RMB: ¥ " + s)); //只关注输入输出, 不关注细节 } } class A { private int m; public A(int m) { this.m = m; } public void pM(Function<Integer, String> mf) { System.out.println("--test--" + mf.apply(m)); } }
--test-- RMB: ¥ 1,234,567,890
- 方法引用
package com.yu.lambda; import java.util.function.*; public class TestMethodRefrence { public static void main(String[] args) throws Exception { Consumer<String> c = s -> System.out.println(s); c.accept("p"); Consumer<String> m = System.out::println; m.accept("ps"); Cat cat = new Cat(); Consumer<Cat> q = Cat::sleep; q.accept(cat); //分析 输入, 输出 Function<Integer, Integer> w = cat::eat; System.out.println("food surplus " + w.apply(1)); IntUnaryOperator e = cat::eat; System.out.println("food surplus " + e.applyAsInt(2)); //jdk 默认把当前实例传入到非静态方法 //public int eat(Cat cat, int number) { BiFunction<Cat, Integer, Integer> t = Cat::eat; System.out.println("food surplus " + t.apply(cat, 3)); Supplier<Cat> s = Cat::new; System.out.println("create new cat : " + s.get()); //输入 String, 输出 Cat Function<String, Cat> d = Cat::new; System.out.println("create new cat : " + d.apply("mm")); } } class Cat { private String name; private int food = 10; public Cat() { this.name = "xiaobai"; } public Cat(String name) { this.name = name; } public static void sleep(Cat cat) { System.out.println("cat sleep......"); } public int eat(int number) { System.out.println("cat eat food " + number); this.food -= number; return this.food; } public String toString() { return this.name; } }
p ps cat sleep...... cat eat food 1 food surplus 9 cat eat food 2 food surplus 7 cat eat food 3 food surplus 4 create new cat : xiaobai create new cat : mm
- 类型推断
package com.yu.lambda; public class TestTypeInference { public static void main(String[] args) throws Exception { IMath im = (x, y) -> x + y; System.out.println("x + y = " + im.add(1, 2)); IMath[] ims = { (x, y) -> x + y }; Object obj = (IMath)(x, y) -> x + y; IMath m = getLambda(); new TestTypeInference().testParam((x, y) -> x + y); } public static IMath getLambda() { return (x, y) -> x + y; } public void testParam(IMath math) { System.out.println("x + y = " + math.add(3, 4)); } } @FunctionalInterface interface IMath { int add(int x, int y); }
x + y = 3 x + y = 7
- 变量引用
编译器说 Lambda 表达式中的变量必须是 final 的,我偏不信
函数式编程和lambda表达式--学习笔记
最新推荐文章于 2023-12-21 09:27:30 发布