????Lambda表达式
基础引入
常规基础
最基础的写法过于冗余,于是下面使用Lambda写法进行简化(其中lambda有常用的四种函数式接口)下面案例都是基础写法方便理解
/*基本写法重写接口内方法*/
public class Lambda1{
public static void main(String[] args) {
//重写接口内方法
Inter1 inter1=new Inter1() {
@Override
public void run() {
System.out.println('重写方法~~~');
}
};
inter1.run();
//简化:通过接口的方法参数与返回值进行简化
//1、首先重写方法体如果就一句则可以省略花括号,
//2、其次如果就一个参数可以省略(),
//3、无参数必须带()
//4、方法体就一句且是返回值,return可以省略
Inter1 inter2=()->{
System.out.println('lambda简化------1');
};
Inter1 inter3=()->System.out.println('lambda简化------2');
inter2.run();
inter3.run();
/*四种函数式接口的实现重写
* */
//函数型
InterFun interFun=x-> x*10;//重写方法发
System.out.println('函数型:'+interFun.fun(10));//调用
//供给型
InterGive interGive=()->100;
System.out.println('供给型:'+interGive.give());
//消费型
InterMonery interMonery=x-> System.out.println('消费型:消费了~');
interMonery.Monery(100);//随意传入一个参数
//段言型
InterBoolean interBoolean=x->x>10?true:false;
System.out.println('段言型:'+interBoolean.isTrue(100));
}
}
//编写接口类
interface Inter1{
void run();
}
//函数型
interface InterFun{
int fun(int x);
}
//供给型
interface InterGive{
int give();
}
//消费型
interface InterMonery{
void Monery(int x);
}
//段言型
interface InterBoolean{
boolean isTrue(int x);
}
Lambda方法调用
方法的调用进行简化,还是一样的类名/对象名.方法,不同的是调用的是重写接口的方法。
/*lambda方法*/
public class Lambda2 {
public static void main(String[] args) {
//创建类对象
Lambda2 lambda2=new Lambda2();
//调用接口方法
// 非static修饰的方法需要对象::方法名调用
// static修饰的直接类名::方法名
Inter_1 inter1=lambda2::method_1;
inter1.method(666);
Inter_1 inter_2=Lambda2::method_2;
inter_2.method(666);
}
//重写接口的方法对应
public void method_1(int x){
System.out.println('重写方法method_1');
}
//静态方法重写接口方法
public static void method_2(int x){
System.out.println('重写方法method_2(static版本)');
}
}
interface Inter_1{
void method(int x);
}
构造与实例
构造器Lambda表达式写法,与简化,下面会建一个Dog的类作为测试,以及通过lambda表达式来重写比较器进行集合的排序
/*构造器lambda表达式 */
public class Test02 {
public static void main(String[] args) {
//重写方法
//写法1
fun01 funA=()->{
return new Dog();
};
System.out.println(funA.getDog());
//写法优化
fun01 funB=()->new Dog();
System.out.println(funB.getDog());
fun02 funD=(a,b)->new Dog();
funD.getDog('kk',33);
//写法2(Dog::new最简)
fun02 funC=Dog::new;
System.out.println(funC.getDog('小花',10));
}
}
/*接口一获取一个Dog对象*/
interface fun01{
Dog getDog();
}
/*接口二获取一个有参构造对象*/
interface fun02{
Dog getDog(String name,Integer age);
}
假设一个集合进行集合内数据进行排序
/*
* 对集合排序
* */
public class Demo01 {
public static void main(String[] args) {
List dogs=new ArrayList<>();
dogs.add(new Dog('a',20));
dogs.add(new Dog('b',24));
dogs.add(new Dog('c',18));
dogs.add(new Dog('d',23));
//排序lambda
//此处使用了Comparator super E> c函数式接口比较器中的方法int compare(T o1, T o2);
//通过此方法一个返回值两个参数可以直接进行lambda表达式写法
dogs.sort((dog1,dog2)->dog1.age-dog2.age);
//lambda打印
dogs.forEach(System.out::println);
}
}
这个一般出现在forEach()方法中作为参数,println是PrintStreajava类中的一个方法,out是Systejava类的一个PringtStream类型的常量对象,故Systeout.println是正常的调用写法,但此处println方法作为forEach(Consumeractio的参数,也就是Consumeraction函数式接口的重写方法,也就是其接口内的voidaccept(Tt);方法对应,故写成:集合:forEach(Systeout::printl;
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点