有经验的盐城网站开发,用户图片上传wordpress,编程需要下载什么软件,python 做的网站目录
一、Lambda
1、什么是Lambda表达式
2.Lambda表达式的基本语法#xff1a;
3.参数列表
4.Lambda表达式使用前后对比
举例一#xff1a;
举例二#xff1a;
二、函数式接口
1.函数式接口的使用说明
2.4个基本的函数式接口
3.如何定义函数接口
1.保证接口中只…目录
一、Lambda
1、什么是Lambda表达式
2.Lambda表达式的基本语法
3.参数列表
4.Lambda表达式使用前后对比
举例一
举例二
二、函数式接口
1.函数式接口的使用说明
2.4个基本的函数式接口
3.如何定义函数接口
1.保证接口中只能有一个抽象方法
2.使用FunctionalInterface注解标记该接口为函数接口
4.Lambda表达式调用方式
1.使用Lambda调用无参函数
2.使用Lambda调用有参函数
3.使用Lambda的精简写法
4.使用Lambda实现集合遍历
5.使用Lambda实现集合排序
6.使用Lambda实现线程调用
一、Lambda
1、什么是Lambda表达式
Lambda 表达式是 Java 8 引入的一种新的语法特性它可以使得代码更加简洁、易读并且支持函数式编程。Lambda 表达式实际上就是一个匿名方法它可以作为参数传递给方法或者存储在变量中。
2.Lambda表达式的基本语法 * 1.举例 (o1,o2) - Integer.compare(o1,o2); * 2.格式 * - :lambda操作符 或 箭头操作符 * -左边lambda形参列表 其实就是接口中的抽象方法的形参列表 * -右边lambda体 其实就是重写的抽象方法的方法体 3.参数列表 4.Lambda表达式使用前后对比
举例一
Test
public void test1(){Runnable r1 new Runnable() {Overridepublic void run() {System.out.println(我爱北京天安门);}};r1.run();System.out.println(***********************);Runnable r2 () - System.out.println(我爱北京故宫);r2.run();
}
举例二
Test
public void test2(){ComparatorInteger com1 new ComparatorInteger() {Overridepublic int compare(Integer o1, Integer o2) {return Integer.compare(o1,o2);}};int compare1 com1.compare(12,21);System.out.println(compare1);System.out.println(***********************);//Lambda表达式的写法ComparatorInteger com2 (o1,o2) - Integer.compare(o1,o2);int compare2 com2.compare(32,21);System.out.println(compare2);System.out.println(***********************);//方法引用ComparatorInteger com3 Integer :: compare;int compare3 com3.compare(32,21);System.out.println(compare3);
}
二、函数式接口
1.函数式接口的使用说明 如果一个接口中只声明了一个抽象方法则此接口就称为函数式接口。 我们可以在一个接口上使用 FunctionalInterface 注解这样做可以检查它是否是一个函数式接口。 Lambda 表达式的函数体可以是一个表达式也可以是一个语句块。如果函数体只有一条表达式可以直接写在箭头后面如果函数体包含多条语句需要使用大括号 {} 将它们括起来并使用分号 ; 分隔。 Lambda表达式的本质作为函数式接口的实例 2.4个基本的函数式接口
Java8中关于Lambda表达式提供的4个基本的函数式接口具体使用 3.如何定义函数接口
定义一个函数接口需要满足以下两点
1.保证接口中只能有一个抽象方法
2.使用FunctionalInterface注解标记该接口为函数接口
FunctionalInterface
public interface MyInterfaceByJava8 {void add();//只能存在一个抽象方法
}
4.Lambda表达式调用方式
1.使用Lambda调用无参函数
首先定义一个函数接口
FunctionalInterface
public interface FuncInterface {void add();
}
接着实现并调用这个无参函数这里展示传统的匿名内部类调用法跟Lambda调用法。
public static void main(String [] args){//使用匿名内部类调用new FuncInterface(){Overridepublic void add() {System.out.println(匿名内部类实现调用);}}.add();//使用Lambda调用((FuncInterface)()-{System.out.println(Lambda实现调用);}).add();}
可以看到使用Lambda代码更加简洁优雅
2.使用Lambda调用有参函数
定义函数接口
FunctionalInterface
public interface FuncInterface2 {int del(int id);
}
实现并调用
public static void main(String [] args){int id101;//使用匿名内部类调用int result1new FuncInterface2(){Overridepublic int del(int id) {System.out.println(匿名内部类实现调用:id);return 1;}}.del(id);//使用Lambda调用int result2((FuncInterface2)(p)-{System.out.println(Lambda实现调用:p);return 1;}).del(id);System.out.println(result1);System.out.println(result2);}
3.使用Lambda的精简写法
当函数参数仅有一个时可以省略括号没有参数时必须用空括号不能省
//普通写法
int result2((FuncInterface2)(p)-{System.out.println(Lambda实现调用:p);return 1;}).del(id);//精简写法--省略参数园括号
int result2((FuncInterface2)p-{System.out.println(Lambda实现调用:p);return 1;}).del(id);
当函数体只有一条语句可以省略{}
//普通写法
((FuncInterface)()-{System.out.println(Lambda实现调用);}).add();//精简写法--省略函数体括号
((FuncInterface)()-System.out.println(Lambda实现调用)).add();
当函数体仅有一条语句且是返回语句时可以省略return
//普通写法
int result2((FuncInterface2)p-return 1).del(id);//精简写法--省略return
int result2((FuncInterface2)p-1).del(id);
4.使用Lambda实现集合遍历
public static void main(String [] args){ListString namesArrays.asList(小明,小黑,小红);//传统遍历--增强forfor(String name:names){System.out.println(name);}//传统遍历--匿名内部类names.forEach(new ConsumerString(){Overridepublic void accept(String name) {System.out.println(name);}});//Lambda表达式遍历names.forEach(name-System.out.println(name));
}
5.使用Lambda实现集合排序
将学生集合的学生按照年龄排序
public static void main(String [] args){ListStudent students1Arrays.asList(new Student(小明,18),new Student(小黑,16),new Student(小红,17));ListStudent students2Arrays.asList(new Student(小明,18),new Student(小黑,16),new Student(小红,17));//传统排序--匿名内部类students1.sort(new ComparatorStudent(){Overridepublic int compare(Student stu1, Student stu2) {return stu1.getAge()- stu2.getAge();}});//Lambdastudents2.sort((stu1,stu2)-stu1.getAge()-stu2.getAge());//省略returnSystem.out.println(students1);System.out.println(students2);
}
6.使用Lambda实现线程调用
public static void main(String [] args){//传统调用new Thread(new Runnable(){Overridepublic void run() {System.out.println(子线程运行了);}}).start();//Lambdanew Thread(()-System.out.println(子线程运行了)).start();
}