函数式编程和lambda表达式--学习笔记

  1. 认识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

  2. 函数接口
    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

  3. 方法引用
    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

  4. 类型推断
    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

  5. 变量引用
    编译器说 Lambda 表达式中的变量必须是 final 的,我偏不信
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值