lambda表达式

1.1

package com.lam;

@FunctionalInterface
interface mulInt{
    int result(int a);
    //1.8后默认方法
    default int result(int x,int y){
        return x*y;
    }
}

public class LambdaDemo {

    public static void main(String[] args) {

        mulInt b=(i)->2*i;
        mulInt a=i->2*i;
        System.out.println(b.result(1));

        System.out.println(b.result(2,2));
    }

}

 

  1. 1.2自定义函数编程
package com.lam;

import java.text.DecimalFormat;

interface PrintMoney{
    String formter(int i);
}

class MoneyProform{

    private final int money;

    public MoneyProform(int money) {
        this.money = money;
    }

    public void printMoney(PrintMoney  printMoney){
        System.out.println(printMoney.formter(money));
    }
}


public class LambDaDemo1 {

    public static void main(String[] args) {
        MoneyProform moneyProform = new MoneyProform(9999999);
        moneyProform.printMoney(i->new DecimalFormat("#,###").format(i));
    }

}

 

  1. 函数function
package com.lam;

import java.text.DecimalFormat;
import java.util.function.Function;

//interface PrintMoney{
//    String formter(int i);
//}

class MoneyProform{

    private final int money;

    public MoneyProform(int money) {
        this.money = money;
    }

    public void printMoney(Function moneyFormat){
        System.out.println(moneyFormat.apply(this.money));
    }
}


public class LambDaDemo1 {

    public static void main(String[] args) {
        MoneyProform moneyProform = new MoneyProform(9999999);
        moneyProform.printMoney(i->new DecimalFormat("#,###").format(i));
    }

}

 

2.函数式流式编程

package com.lam;

import java.text.DecimalFormat;
import java.util.function.Function;

//interface PrintMoney{
//    String formter(int i);
//}

class MoneyProform{

    private final int money;

    public MoneyProform(int money) {
        this.money = money;
    }

    public void printMoney(Function moneyFormat){
        System.out.println(moneyFormat.apply(this.money));
    }
}


public class LambDaDemo1 {

    public static void main(String[] args) {
        MoneyProform moneyProform = new MoneyProform(9999999);
        Function integerStringFunction = i -> new DecimalFormat("#,###").format(i);
        moneyProform.printMoney(integerStringFunction.andThen(s->"人民币"+s));
    }

}

你可能感兴趣的:(lambda表达式)