目录
一、Lambda知识
一、Lambda表达式的介绍
Lambda表达式是 Java8 中最重要的新功能之一。使用 Lambda 表达 式可以替代只有一个抽象函数的接口实现,告别匿名内部类,代码看 起来更简洁易懂。Lambda表达式同时还提升了对集合、框架的迭代、 遍历、过滤数据的操作
二、Lambda的特点
1:函数式编程
2:参数类型自动推断
3:代码量少,简洁
三、Lambda表达式应用场景
任何有函数式接口的地方
四、Lambda表达式的使用、函数式接口
只有一个抽象方法(Object类中的方法除外)的接口是函数式接口
Supplier 代表一个输出
Consumer 代表一个输入
BiConsumer 代表两个输入
Function 代表一个输入,一个输出(一般输入和输出是不同类型的)
UnaryOperator 代表一个输入,一个输出(输入和输出是相同类型的)
BiFunction 代表两个输入,一个输出(一般输入和输出是不同类型的)
BinaryOperator 代表两个输入,一个输出(输入和输出是相同类型的
五、Lambda表达式的引用
1、方法的引用
方法引用是用来直接访问类或者实例的已经存在的方法或者构造 方法,方法引用提供了一种引用而不执行方法的方式,如果抽象 方法的实现恰好可以使用调用另外一个方法来实现,就有可能可 以使用方法引用
2、方法引用的分类
▪ 静态方法引用:如果函数式接口的实现恰好可以通过调用一个静 态方法来实现,那么就可以使用静态方法引用
▪ 实例方法引用:如果函数式接口的实现恰好可以通过调用一个实 例的实例方法来实现,那么就可以使用实例方法引用
▪ 对象方法引用:抽象方法的第一个参数类型刚好是实例方法的类 型,抽象方法剩余的参数恰好可以当做实例方法的参数。如果函 数式接口的实现能由上面说的实例方法调用来实现的话,那么就 可以使用对象方法引用
▪ 构造方法引用:如果函数式接口的实现恰好可以通过调用一个类 的构造方法来实现,那么就可以使用构造方法引用
二、代码
一、Lambda
1、LambdaTest
@Auther:kongshan
@Date:2022/5/3-05-03-17:12
@Description:IntelliJ IDEA
@version:
*/
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class LambdaTest {
public static void main(String[] args) throws Exception {
Runnable runnable=new Runnable() {
@Override
public void run() {
System.out.println("running1");
}
};
runnable.run();
Runnable runnable1=()-> System.out.println("running2");
Runnable runnable2=()->{
System.out.println("running3");
};
runnable1.run();
runnable2.run();
Callable<String>callable=new Callable<String>() {
@Override
public String call() throws Exception {
return "空衫";
}
};
System.out.println(callable.call());
Callable<String>callable1=()->{return "空衫2";};
Callable<String>callable2=()->"空衫3";
System.out.println(callable1.call());
System.out.println(callable2.call());
StudentDao dao=new StudentDao() {
@Override
public void insert(Student student) {
System.out.println("插入一个学生记录:"+student);
}
};
dao.insert(new Student());
StudentDao dao1=(student) -> System.out.println("插入:"+student);
StudentDao dao2=(Student student) -> System.out.println("插入:"+student);
StudentDao dao3=(student) ->{
System.out.println("插入:"+student);
};
StudentDao dao4=(Student student) ->{System.out.println("插入:"+student);};
StudentDao dao5=student -> System.out.println("插入:"+student);
StudentDao dao6=student -> {
System.out.println("插入:"+student);
};
dao1.insert(new Student());
dao2.insert(new Student());
dao3.insert(new Student());
dao4.insert(new Student());
dao5.insert(new Student());
dao6.insert(new Student());
TeacherDao teacherDao=new TeacherDao() {
@Override
public int get(Teacher teacher) {
return 1;
}
};
System.out.println(teacherDao.get(new Teacher()));
TeacherDao teacherDao1=(teacher) -> {return 2;};
TeacherDao teacherDao2=(teacher) -> 3;
TeacherDao teacherDao3=teacher -> {return 4;};
TeacherDao teacherDao4=teacher -> 5;
System.out.println(teacherDao1.get(new Teacher()));
System.out.println(teacherDao2.get(new Teacher()));
System.out.println(teacherDao3.get(new Teacher()));
System.out.println(teacherDao4.get(new Teacher()));
/*
* 在JDK中,提供了一些代表输入和输出的接口,他们本身是没有什么意义的,
* 只是为了方便的使用lambda表达式来编写代码,从而获取对应的执行结果
* */
Supplier<Integer>supplier=new Supplier<Integer>() {
@Override
public Integer get() {
return 1;
}
};
Supplier<Integer>supplier1=()->1;
System.out.println(supplier1.get());
Consumer<String>consumer=new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
Consumer<String>consumer1=(s)-> System.out.println(s);
consumer1.accept("空衫");
Function<Integer,String>function=new Function<Integer, String>() {
@Override
public String apply(Integer integer) {
return integer+"空衫";
}
};
Function<Integer,String>function1=(i)->i+"空衫";
Function<Integer,String>function2=(Integer i)->{return i+"空衫";};
System.out.println(function1.apply(200));
System.out.println(function2.apply(300));
}
}
2、 StudentDao
@Auther:kongshan
@Date:2022/5/3-05-03-17:18
@Description:IntelliJ IDEA
@version:
*/
@FunctionalInterface
public interface StudentDao {
public void insert(Student student);
}
3、Student
@Auther:kongshan
@Date:2022/5/3-05-03-17:19
@Description:IntelliJ IDEA
@version:
*/
public class Student {
}
4、Teacher
@Auther:kongshan
@Date:2022/5/3-05-03-17:55
@Description:IntelliJ IDEA
@version:
*/
public class Teacher {
}
5、TeacherDAO
@Auther:kongshan
@Date:2022/5/3-05-03-17:54
@Description:IntelliJ IDEA
@version:
*/
@FunctionalInterface
public interface TeacherDao {
public int get(Teacher teacher);
}
二、why1
1、Student
@Auther:kongshan
@Date:2022/5/3-05-03-18:15
@Description:IntelliJ IDEA
@version:
*/
public class Student {
private String name;
private int age;
private int score;
public Student() {
}
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
2、Test
@Auther:kongshan
@Date:2022/5/3-05-03-18:16
@Description:IntelliJ IDEA
@version:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Student>list=new ArrayList<>();
list.add(new Student("张飞",18,69));
list.add(new Student("刘备",20,75));
list.add(new Student("曹操",26,70));
list.add(new Student("赵云",30,85));
list.add(new Student("黄忠",50,90));
//查询年龄大于25岁的学生信息、
getByAge(list);
//查询分数大于80分的学生信息
System.out.println("***************");
getByScore(list);
}
public static void getByAge(ArrayList<Student>students){
ArrayList<Student>list=new ArrayList<>();
for (Student student:students){
if (student.getAge()>25){
list.add(student);
}
}
for (Student student:list){
System.out.println(student);
}
}
public static void getByScore(ArrayList<Student>students){
ArrayList<Student>list=new ArrayList<>();
for (Student student:students){
if (student.getScore()>80){
list.add(student);
}
}
for (Student student:list){
System.out.println(student);
}
}
}
三、why2
1、AgeFilter
@Auther:kongshan
@Date:2022/5/3-05-03-18:30
@Description:IntelliJ IDEA
@version:
import com.tensent.class4_Lambda表达式.why1.Student;
public class AgeFilter implements StudentFilter {
@Override
public boolean compare(Student student) {
return student.getAge()>25;
}
}
2、ScoreFilter
@Auther:kongshan
@Date:2022/5/3-05-03-18:31
@Description:IntelliJ IDEA
@version:
import com.tensent.class4_Lambda表达式.why1.Student;
public class ScoreFiler implements StudentFilter {
@Override
public boolean compare(Student student) {
return student.getScore()>80;
}
}
3、StudentFilter
@Auther:kongshan
@Date:2022/5/3-05-03-18:28
@Description:IntelliJ IDEA
@version:
import com.tensent.class4_Lambda表达式.why1.Student;
public interface StudentFilter {
public boolean compare(Student student);
}
4、Test
@Auther:kongshan
@Date:2022/5/3-05-03-19:20
@Description:IntelliJ IDEA
@version:
import com.tensent.class4_Lambda表达式.why1.Student;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<>();
list.add(new Student("张飞",18,69));
list.add(new Student("刘备",20,75));
list.add(new Student("曹操",26,70));
list.add(new Student("赵云",30,85));
list.add(new Student("黄忠",50,90));
//查询年龄大于25岁的学生信息、
getByFilter(list,new AgeFilter());
//查询分数大于80分的学生信息
System.out.println("***************");
getByFilter(list,new ScoreFiler());
}
public static void getByFilter(ArrayList<Student>students, StudentFilter filter){
ArrayList<Student>list=new ArrayList<>();
for (Student student:students){
if (filter.compare(student)){
list.add(student);
}
}
for (Student student:list){
System.out.println(student);
}
}
}
四、why3
1、Test
@Auther:kongshan
@Date:2022/5/3-05-03-19:27
@Description:IntelliJ IDEA
@version:
import com.tensent.class4_Lambda表达式.why1.Student;
import com.tensent.class4_Lambda表达式.why2.StudentFilter;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<>();
list.add(new Student("zhangfei",18,69));
list.add(new Student("liubei",20,75));
list.add(new Student("caocao",26,70));
list.add(new Student("zhaoyun",30,85));
list.add(new Student("huangzhong",50,90));
//查询年龄大于25岁的学生信息、
getByFilter(list, new StudentFilter() {
@Override
public boolean compare(Student student) {
return student.getAge()>25;
}
});
//查询分数大于80分的学生信息
System.out.println("***************");
getByFilter(list, new StudentFilter() {
@Override
public boolean compare(Student student) {
return student.getScore()>80;
}
});
//查询姓名长度大于6的学生信息
System.out.println("********");
getByFilter(list, new StudentFilter() {
@Override
public boolean compare(Student student) {
return student.getName().length()>6;
}
});
}
public static void getByFilter(ArrayList<Student>students, StudentFilter filter){
ArrayList<Student>list=new ArrayList<>();
for (Student student:students){
if (filter.compare(student)) {
list.add(student);
}
}
PrintStudent(list);
}
public static void PrintStudent(ArrayList<Student>students){
for (Student student:students){
System.out.println(student);
}
}
}
五、why4
1、Test
@Auther:kongshan
@Date:2022/5/3-05-03-19:40
@Description:IntelliJ IDEA
@version:
import com.tensent.class4_Lambda表达式.why1.Student;
import com.tensent.class4_Lambda表达式.why2.StudentFilter;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("zhangfei", 18, 69));
list.add(new Student("liubei", 20, 75));
list.add(new Student("caocao", 26, 70));
list.add(new Student("zhaoyun", 30, 85));
list.add(new Student("huangzhong", 50, 90));
//查询年龄大于25岁的学生信息、
getByFilter(list,(student)->student.getAge()>25);
//查询分数大于80分的学生信息
System.out.println("********************");
getByFilter(list,(student)->student.getScore()>80);
//查询姓名长度大于6的学生信息
System.out.println("********************");
getByFilter(list,(student)->student.getName().length()>6);
}
public static void getByFilter(ArrayList<Student>students, StudentFilter filter) {
ArrayList<Student> list = new ArrayList<>();
for (Student student : students) {
if (filter.compare(student))
list.add(student);
}
printStudent(list);
}
public static void printStudent(ArrayList<Student>students){
for (Student student:students){
System.out.println(student);
}
}
}
六、Lambda
1、LambdaDemo
public class LambdaDemo {
public static void main(String[] args) {
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("空衫");
}
});
thread.start();
new Thread(()-> System.out.println("空衫!")).start();
List<String> list= Arrays.asList("java","go","python","scala","javascript");
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length()-o2.length();
}
});
for (String str:list){
System.out.println(str);
}
Collections.sort(list,(a,b)->a.length()-b.length());
list.forEach(System.out::println);
}
}
2、LambdaInterface
@Auther:kongshan
@Date:2022/5/3-05-03-17:28
@Description:IntelliJ IDEA
@version:
@FunctionalInterface
//注解 只能有一个函数
public interface Lamdbalnterface {
public void add(int a, int b);
// public void sub(int a, int b);
}
七、
1、LambdaTest
@Auther:kongshan
@Date:2022/5/4-05-04-9:41
@Description:IntelliJ IDEA
@version:
import com.tensent.class4_Lambda表达式.Lambda.Student;
import com.tensent.class4_Lambda表达式.Lambda.StudentDao;
import com.tensent.class4_Lambda表达式.Lambda.Teacher;
import com.tensent.class4_Lambda表达式.Lambda.TeacherDao;
import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class LambdaTest {
public static void main(String[] args) throws Exception {
Runnable runnable1=new Runnable() {
@Override
public void run() {
System.out.println("running!");
}
};
runnable1.run();
Runnable runnable2=()-> System.out.println("running2!");
Runnable runnable3=()->{
System.out.println("running3");
};
runnable2.run();
runnable3.run();
Callable<String>callable=new Callable<String>() {
@Override
public String call() throws Exception {
return "空衫";
}
};
System.out.println(callable.call());
Callable<String>callable1=()->{return "空衫2";};
Callable<String>callable2=()->"空衫3";
System.out.println(callable1.call());
System.out.println(callable2.call());
StudentDao dao=new StudentDao() {
@Override
public void insert(Student student) {
System.out.println("插入1个学生记录"+student);
}
};
dao.insert(new Student());
StudentDao dao1=(student)-> System.out.println("插入"+student);
StudentDao dao2=(Student student) -> System.out.println("插入:"+student);
StudentDao dao3=(student)->{
System.out.println("插入:"+student);
};
StudentDao dao4=(Student student)->{
System.out.println("插入"+student);
};
StudentDao dao5=student -> System.out.println("插入:"+student);
StudentDao dao6=student -> {System.out.println("插入:"+student);};
dao1.insert(new Student());
dao2.insert(new Student());
dao3.insert(new Student());
dao4.insert(new Student());
dao5.insert(new Student());
dao6.insert(new Student());
TeacherDao teacherDao=new TeacherDao() {
@Override
public int get(Teacher teacher) {
return 1;
}
};
System.out.println(teacherDao.get(new Teacher()));
TeacherDao teacherDao1=(teacher)->{return 2;};
TeacherDao teacherDao2=(teacher)->3;
TeacherDao teacherDao3=teacher -> {return 4;};
TeacherDao teacherDao4=teacher -> 5;
System.out.println(teacherDao1.get(new Teacher()));
System.out.println(teacherDao2.get(new Teacher()));
System.out.println(teacherDao3.get(new Teacher()));
System.out.println(teacherDao4.get(new Teacher()));
/*
* 在JDK中,提供了一些代表输入和输出的接口,他们本身是没有什么意义的,
* 只是为了方便的使用lambda表达式来编写代码,从而获得对应的执行结果*/
Supplier<Integer>supplier=new Supplier<Integer>() {
@Override
public Integer get() {
return 1;
}
};
Supplier<Integer>supplier1=()->1;
System.out.println(supplier1.get());
Consumer<String>consumer=new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
Consumer<String>consumer1=(s)-> System.out.println(s);
consumer.accept("空衫");
Function<Integer,String>function=new Function<Integer, String>() {
@Override
public String apply(Integer integer) {
return integer+"空衫2";
}
};
Function <Integer,String>function1=(i)->i+"空衫3";
Function<Integer,String>function2=(Integer i)->{return i+"空衫4";};
System.out.println(function1.apply(200));
System.out.println(function2.apply(300));
BiFunction<String,String,Integer>biFunction=(a,b)->a.length()+b.length();
System.out.println(biFunction.apply("空衫","贼六"));
Runnable runnablea=()-> System.out.println(get());
Runnable runnableb=()->{
String string=find();
System.out.println(string);
};
Runnable runnablec=()->exec();
// Runnable runnabled=()->1;
// Runnable runnablee=()->"ok";
Runnable runnablef=()->get();
// Runnable runnableg=()->true?1:2;
runnablea.run();
runnableb.run();
runnablec.run();
runnablef.run();
Lambdalnterface li=new Lambdalnterface() {
@Override
public int get(int num) {
return num;
}
};
System.out.println(li.get(200));
Lambdalnterface li1=(num)->get();
// Lambdalnterface li2 = (num)-> System.out.println(num);
// Lambdalnterface li3 = (num)-> {return get(num);};
Lambdalnterface li4 = (num)-> {return num;};
Lambdalnterface li5 = (num)-> num;
Lambdalnterface li6 = num-> num;
Lambdalnterface li7 = num-> {return num;};
System.out.println(li4.get(300));
Function<Integer,Integer> function9 = (num)->num;
System.out.println(function9.apply(100));
Consumer<Integer> consumer8 = (num)->System.out.println(num);
consumer8.accept(1000);
Consumer<Integer> consumer2 = (num)->getNum(num);
consumer2.accept(2000);
Function<String,String> f1 = (str)->str.toUpperCase();
Function<String,String> f2 = (str)->toUpperCase(str);
System.out.println(f1.apply("abcdefg"));
System.out.println(f2.apply("abcdefgefg"));
BiFunction<String,String,Integer> bf = (a,b)->a.length()+b.length();
System.out.println(bf.apply("空衫", "贼六"));
BiFunction<String,String,Integer> bf1 = (a,b)->getStrLength(a,b);
System.out.println(bf1.apply("空衫", "贼六"));
}
public static void getNum(int num){
System.out.println(num);
}
public static String toUpperCase(String str){
return str.toUpperCase();
}
public static Integer getStrLength(String str1,String str2){
return str1.length()+str2.length();
}
public static int get(){
return 1;
}
public static String find(){
return "find";
}
public static void exec(){
find();
}
}
2、Lambdalnterface
@Auther:kongshan
@Date:2022/5/4-05-04-9:40
@Description:IntelliJ IDEA
@version:
*/
@FunctionalInterface
public interface Lambdalnterface {
int get(int num);
}